国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

java編程題(一)

2019-11-14 23:58:27
字體:
來源:轉載
供稿:網友
java編程題(一)

【程序1】 題目:古典問題:有一對兔子,從出生后第3個月起每個月都生一對兔子,小兔子長到第三個月后每個月又生一對兔子,假如兔子都不死,問每個月的兔子總數為多少?

//這是一個菲波拉契數列問題

public class lianxi01 {

public static void main(String[] args) {

System.out.

System.out.println("第2個月的兔子對數: 1");

int f1 = 1, f2 = 1, f, M=24;

for(int i=3; i<=M; i++) {

f = f2;

f2 = f1 + f2;

f1 = f;

System.out.println("第" + i +"個月的兔子對數: "+f2);

}

}

}

【程序2】 題目:判斷101-200之間有多少個素數,并輸出所有素數。 程序分析:判斷素數的方法:用一個數分別去除2到sqrt(這個數),如果能被整除, 則表明此數不是素數,反之是素數。

public class lianxi02 {

public static void main(String[] args) {

int count = 0;

for(int i=101; i<200; i+=2) {

boolean b = false;

for(int j=2; j<=Math.sqrt(i); j++) {

if(i % j == 0) {

b = false; break;

} else{

b = true;

}

}

if(b == true) {

count ++;System.out.println(i );

}

}

System.out.println( "素數個數是: " + count);

}

}

【程序3】 題目:打印出所有的 "水仙花數 ",所謂 "水仙花數 "是指一個三位數,其各位數字立方和等于該數本身。例如:153是一個 "水仙花數 ",因為153=1的三次方+5的三次方+3的三次方。

public class lianxi03 {

public static void main(String[] args) {

int b1, b2, b3;

for(int m=101; m<1000; m++) {

b3 = m / 100;

b2 = m % 100 / 10;

b1 = m % 10;

if((b3*b3*b3 + b2*b2*b2 + b1*b1*b1) == m) {

System.out.println(m+"是一個水仙花數");

}

}

}

}

【程序4】 題目:將一個正整數分解質因數。例如:輸入90,打印出90=2*3*3*5。

程序分析:對n進行分解質因數,應先找到一個最小的質數k,然后按下述步驟完成:

(1)如果這個質數恰等于n,則說明分解質因數的過程已經結束,打印出即可。

(2)如果n <> k,但n能被k整除,則應打印出k的值,并用n除以k的商,作為新的正整數你n,重復執行第一步。

(3)如果n不能被k整除,則用k+1作為k的值,重復執行第一步。

import java.util.*; public

class lianxi04{

public static void main(String[] args) {

Scanner s = new Scanner(System.in);

System.out.print( "請鍵入一個正整數: ");

int n = s.nextInt();

int k=2;

System.out.print(n + "=" );

while(k <= n) {

if(k == n) {System.out.println(n);break;

} else if( n % k == 0) {

System.out.print(k + "*");n = n / k;

}

else k++;

}

}

}

【程序5】 題目:利用條件運算符的嵌套來完成此題:學習成績> =90分的同學用A表示,60-89分之間的用B表示,60分以下的用C表示。

import java.util.*;

public class lianxi05 {

public static void main(String[] args) {

int x;

char grade;

Scanner s = new Scanner(System.in);

System.out.print( "請輸入一個成績: ");

x = s.nextInt();

grade = x >= 90 ? 'A' : x >= 60 ? 'B' :'C';

System.out.println("等級為:"+grade);

}

}

【程序6】 題目:輸入兩個正整數m和n,求其最大公約數和最小公倍數。

/**在循環中,只要除數不等于0,用較大數除以較小的數,將小的一個數作為下一輪循環的大數,取得的余數作為下一輪循環的較小的數,如此循環直到較小的數的值為0,返回較大的數,此數即為最大公約數,最小公倍數為兩數之積除以最大公約數。* / import java.util.*;

public class lianxi06 {

public static void main(String[] args) {

int a ,b,m;

Scanner s = new Scanner(System.in);

System.out.print( "鍵入一個整數: ");

a = s.nextInt(); System.out.print( "再鍵入一個整數: ");

b = s.nextInt();

deff cd = new deff();

m = cd.deff(a,b);

int n = a * b / m;

System.out.println("最大公約數: " + m);

System.out.println("最小公倍數: " + n);

}

}

class deff{

public int deff(int x, int y) {

int t;

if(x < y) {

t = x; x = y; y = t;

} while(y != 0) {

if(x == y) return x;

else {

int k = x % y;

x = y;

y = k;

}

}

return x;

}

}

【程序7】 題目:輸入一行字符,分別統計出其中英文字母、空格、數字和其它字符的個數。

package WuYang; import java.util.*;

public class lianxi07 {

public static void main(String[] args) {

int abcCount=0;//英文字母個數

int spaceCount=0;//空格鍵個數

int numCount=0;//數字個數

int otherCount=0;//其他字符個數

Scanner scan = new Scanner(System.in);//掃描器接受控制臺的輸入信息

System.out.println("輸入一組字符");

String str=scan.nextLine();//取出控制臺的一行信息,也就是你輸入的信息

char[] ch = str.toCharArray();//把取道的字符串變成一個char數組

for(int i=0;i<ch.length;i++){

if(Character.isLetter(ch[i])){

//判斷是否字母

abcCount++;

} else if(

Character.isDigit(ch[i])){

//判斷是否數字

numCount++;

} else if(

Character.isSpaceChar(ch[i])){

//判斷是否空格鍵

spaceCount++;

} else{

//以上都不是則認為是其他字符

otherCount++;

}

}

System.out.println("字母個數:"+abcCount);

System.out.println("數字個數:"+numCount);

System.out.println("空格個數:"+spaceCount);

System.out.println("其他字符個數:"+otherCount);

}

}

【程序8】 題目:輸入三個整數x,y,z,請把這三個數由小到大輸出。

import java.util.*;

public class lianxi15 {

public static void main(String[] args) {

input fnc = new input();

int x=0, y=0, z=0;

System.out.print("輸入第一個數字:");

x = fnc.input();

System.out.print("輸入第二個數字:");

y = fnc.input();

System.out.print("輸入第三個數字:");

z = fnc.input();

if(x > y) {

int t = x;

x = y;

y = t;

}

if(x > z) {

int t = x;

x = z;

z = t;

}

if(y > z) {

int t = y;

y = z;

z = t;

}

System.out.println( "三個數字由小到大排列為: "+x + " " + y + " " + z);

}

} class input{

public int input() {

int value = 0;

Scanner s = new Scanner(System.in);

value = s.nextInt();

return value;

}

}

10 【程序9】 題目:輸出9*9口訣。

public class lianxi16 {

public static void main(String[] args) {

for(int i=1; i<10; i++) {

for(int j=1; j<=i; j++) {

System.out.print(j + "*" + i + "=" + j*i + " "

);

if(j*i<10){System.out.print(" ");

}

}

System.out.println();

}

}

}

【程序10】 題目:猴子吃桃問題:猴子第一天摘下若干個桃子,當即吃了一半,還不癮,又多吃了一個 第二天早上又將剩下的桃子吃掉一半,又多吃了一個。以后每天早上都吃了前一天剩下 的一半零一個。到第10天早上想再吃時,見只剩下一個桃子了。求第一天共摘了多少。

public class lianxi17 {

public static void main(String[] args) {

int x = 1;

for(int i=2; i<=10; i++) {

x = (x+1)*2;

}

System.out.println("猴子第一天摘了 " + x + " 個桃子");

}

}

【程序11】 題目:一個數如果恰好等于它的因子之和,這個數就稱為 "完數 "。例如6=1+2+3.編程 找出1000以內的所有完數。

public class lianxi09 {

public static void main(String[] args) {

System.out.println("1到1000的完數有: ");

for(int i=1; i<1000; i++) {

int t = 0;

for(int j=1; j<= i/2; j++) {

if(i % j == 0) {

t = t + j;

}

}

if(t == i) {

System.out.print(i + " ");

}

}

}

}

【程序12】 題目:一球從100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在 第10次落地時,共經過多少米?第10次反彈多高?

public class lianxi10 {

public static void main(String[] args) {

double h = 100,s = 0;

for(int i=1; i<=10; i++) {

s = s + 2*h;

h = h / 2;

}

s=s-100;

System.out.println("經過路程:" + s);

System.out.println("最后高度:" + h);

}

}

【程序13】 題目:有1、2、3、4四個數字,能組成多少個互不相同且一個數字中無重復數字的三位數?并把他們都輸入。

public class lianxi11 {

public static void main(String[] args) {

int count = 0;

for(int x=1; x<5; x++) {

for(int y=1; y<5; y++) {

for(int z=1; z<5; z++) {

if(x != y && y != z && x != z) {

count ++;

System.out.println(x*100 + y*10 + z );

}

}

}

}

System.out.println("共有" + count + "個三位數");

}

}

【程序14】 題目:企業發放的獎金根據利潤提成。利潤(I)低于或等于10萬元時,獎金可提10%;利潤高于10萬元,低于20萬元時,低于10萬元的部分按10%提成,高于10萬元的部分,可可提成7.5%;20萬到40萬之間時,高于20萬元的部分,可提成5%;40萬到60萬之間時高于40萬元的部分,可提成3%;60萬到100萬之間時,高于60萬元的部分,可提成1.5%,高于100萬元時,超過100萬元的部分按1%提成,從鍵盤輸入當月利潤,求應發放獎金總數?

import java.util.*; public class lianxi12 {

public static void main(String[] args) {

double x = 0,y = 0;

System.out.print("輸入當月利潤(萬):");

Scanner s = new Scanner(System.in);

x = s.nextInt();

if(x > 0 && x <= 10) {

y = x * 0.1;

} else if(x > 10 && x <= 20) {

y = 10 * 0.1 + (x - 10) * 0.075;

} else if(x > 20 && x <= 40) {

y = 10 * 0.1 + 10 * 0.075 + (x - 20) * 0.05;

} else if(x > 40 && x <= 60) {

y = 10 * 0.1 + 10 * 0.075 + 20 * 0.05 + (x - 40) * 0.03;

} else if(x > 60 && x <= 100) {

y = 20 * 0.175 + 20 * 0.05 + 20 * 0.03 + (x - 60) * 0.015;

} else if(x > 100) {

y = 20 * 0.175 + 40 * 0.08 + 40 * 0.015 + (x - 100) * 0.01;

} System.out.println("應該提取的獎金是 " + y + "萬");

}

}

【程序15】 題目:輸入某年某月某日,判斷這一天是這一年的第幾天?

import java.util.*; public class lianxi14 {

public static void main(String[] args) {

int year, month, day;

int days = 0;

int d = 0;

int e;

input fymd = new input();

do {

e = 0;

System.out.print("輸入年:");

year =fymd.input();

System.out.print("輸入月:");

month = fymd.input();

System.out.print("輸入天:");

day = fymd.input();

if (year < 0 || month < 0 || month > 12 || day < 0 || day > 31) {

System.out.println("輸入錯誤,請重新輸入!"); e=1

}

}while( e==1); for (int i=1; i <month; i++) {

switch (i) {

case 1:

case 3:

case 5:

case 7:

case 8:

case 10:

case 12:

days = 31;

break;

case 4:

case 6:

case 9:

case 11:

days = 30;

break;

case 2:

if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) {

days = 29;

} else {

days = 28;

}

break;

} d += days;

} System.out.println(year + "-" + month + "-" + day + "是這年的第" + (d+day) + "天。");

}

}

class input{

public int input() {

int value = 0;

Scanner s = new Scanner(System.in);

value = s.nextInt();

return value; }

}

【程序19】 題目:打印出如下圖案(菱形)

*

***

*****

*******

*****

***

*

public class lianxi19 {

public static void main(String[] args) {

int H = 7, W = 7;//高和寬必須是相等的奇數

for(int i=0; i<(H+1) / 2; i++) {

for(int j=0; j<W/2-i; j++) {

System.out.print(" ");

}

for(int k=1; k<(i+1)*2; k++) {

System.out.print('*');

}

System.out.println();

} for(int i=1; i<=H/2; i++) {

for(int j=1; j<=i; j++) {

System.out.print(" ");

} for(int k=1; k<=W-2*i; k++) {

System.out.print('*');

}

System.out.println();

}

}

}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 吐鲁番市| 灵寿县| 廉江市| 晋城| 家居| 察哈| 天全县| 武隆县| 凉城县| 阳谷县| 施甸县| 临江市| 仙居县| 虹口区| 云和县| 比如县| 黔西县| 靖安县| 庆城县| 东源县| 江西省| 永清县| 民乐县| 马关县| 玉山县| 麦盖提县| 义马市| 禄丰县| 民乐县| 新源县| 焉耆| 五指山市| 米泉市| 馆陶县| 乌兰察布市| 绩溪县| 潼南县| 沐川县| 浮山县| 平顶山市| 商洛市|