分支結(jié)構(gòu):
-
寫出結(jié)果:
class Demo{ public static void main(String[] args){ int m=0,n=3; if(m>0) if(n>2) System.out.println("A"); else System.out.println("B"); } } //answer:沒有結(jié)果
實現(xiàn)對三個整數(shù)進(jìn)行排序骑冗,輸出時按照從小到大的順序輸出挽荠。
-
從鍵盤分別輸入年勉耀、月狸捕、日汉操,判斷這一天是當(dāng)年的第幾天
public class Test { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("輸入year:"); int year = scanner.nextInt(); System.out.println("輸入month:"); int month = scanner.nextInt(); System.out.println("輸入day:"); int day = scanner.nextInt(); int sumDay = 0; switch (month) { case 12: sumDay += 30; case 11: sumDay += 31; case 10: sumDay += 30; case 9: sumDay += 31; case 8: sumDay += 31; case 7: sumDay += 30; case 6: sumDay += 31; case 5: sumDay += 30; case 4: sumDay += 31; case 3: if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) sumDay += 29; else sumDay += 28; case 2: sumDay += 31; case 1: sumDay += day; } System.out.println(year + "年" + month + "月" + day + "日是今年的第" + sumDay + "天"); } }
switch是否能作用在byte上淑际,是否能作用在long上走搁,是否能作用在String上
答:switch(expr1)中辛燥,expr1是一個整數(shù)表達(dá)式筛武。因此傳遞給 switch 和 case 語句的參數(shù)應(yīng)該是
int、 short挎塌、 char 或者 byte徘六。long不能作用于swtich.JDK1.7新加入了String類型。-
編寫程序榴都,判斷給定的某個年份是否是閏年
閏年的判斷規(guī)則如下:
(1)若某個年份能被4整除但不能被100整除待锈,則是閏年。
(2)若某個年份能被400整除嘴高,則也是閏年竿音。if((year % 4 ==0 && year % 100 != 0) || year % 400 == 0){}
要求用戶輸入兩個數(shù)a和b,如果a能被b整除或者a加b大于1000拴驮,則輸出a春瞬;否則輸出b。
編寫程序套啤,從鍵盤接收整數(shù)參數(shù).如果該數(shù)為1-7宽气,打印對應(yīng)的星期值,否則打印“非法參數(shù)”潜沦。
使用條件結(jié)構(gòu)實現(xiàn)萄涯,如果用戶名等于字符‘青’,密碼等于數(shù)字‘123’止潮,就輸出“歡迎你窃判,青”,否則就輸出“對不起喇闸,你不是青”袄琳。
提示:先聲明兩個變量,一個是char型的燃乍,用來存放用戶名唆樊,一個是int型的,用來存放密碼刻蟹。-
??求ax2+bx+c=0方程的根逗旁。
a,b,c分別為函數(shù)的參數(shù),
如果:b2-4ac>0,則有兩個解片效;b2-4ac=0红伦,則有一個解;b2-4ac<0淀衣,則無解昙读;
已知:x1=(-b+sqrt(b2-4ac))/2a
X2=(-b-sqrt(b2-4ac))/2aSystem.out.println("求 ax^2 + bx + c = 0的解"); Scanner scan = new Scanner(System.in); while (true) { int a, b, c; a = b = c = 0; //或 int a=0, b=0, c=0; System.out.println("請輸入a的值"); if (scan.hasNextInt()) { a = scan.nextInt(); if (a == 0) { System.out.println("此方程不是2元1次方程,解方程結(jié)束"); return; } } System.out.println("請輸入b的值"); if (scan.hasNextInt()) { b = scan.nextInt(); } System.out.println("請輸入c的值"); if (scan.hasNextInt()) { c = scan.nextInt(); } System.out.println("a=" + a + ", b=" + b + ", c=" + c); double result = b*b - 4 * a * c; System.out.println(result); if (result < 0) { System.out.println("此方程無解"); }else { System.out.println(Math.sqrt(result)); double x1 = (-b + Math.sqrt(result))/(2*a); System.out.println("x1 = " + x1); if (result > 0) { double x2 = (-b - Math.sqrt(result))/(2*a); System.out.println("x2 = " + x2); System.out.println("此方程有2個解"); return; } System.out.println("此方程只有一個解"); } }
生成13位條形碼
```
Ean-13碼規(guī)則:第十三位數(shù)字是前十二位數(shù)字經(jīng)過計算得到的校驗碼膨桥。
例如:690123456789
計算其校驗碼的過程為:
@前十二位的奇數(shù)位和6+0+2+4+6+8=26
@前十二位的偶數(shù)位和9+1+3+5+7+9=34
@將奇數(shù)和與偶數(shù)和的三倍相加26+34*3=128
@取結(jié)果的個位數(shù):128的個位數(shù)為8
@用10減去這個個位數(shù)10-8=2
所以校驗碼為2
(注:如果取結(jié)果的個位數(shù)為0蛮浑,那么校驗碼不是為10(10-0=10),而是0)
實現(xiàn)方法ean13()計算驗證碼只嚣,輸入12位條碼沮稚,返回帶驗證碼的條碼。
例:輸入:692223361219輸出:6922233612192
```
```
//實現(xiàn)代碼
static void test1 () {
Scanner scan = new Scanner(System.in);
String numberString;
while (true) {
System.out.println("輸入12位條碼");
if (scan.hasNext()) {
numberString = scan.next();
//if (number >= 10000000000L && number <= 99999999999L) {//為了讓編譯器直接識別較長的long型册舞,需要直接使用"L"標(biāo)明蕴掏。
if (numberString.length() == 12) {
System.out.println(numberString);
break;//退出循環(huán)
}else {
System.out.println("輸入位數(shù)錯誤");
}
}
}
int sum1 = 0;//奇數(shù)位的和
int sum2 = 0;//偶數(shù)位的和
for (int i = 0; i < 12; i++) {
char letter = numberString.charAt(i);
int iNumber = (int)letter - (int)'0';
if (i%2 == 0) {
//奇數(shù)位
sum1 += iNumber;
}else {
//偶數(shù)位
sum2 += iNumber;
}
}
int checkCode = 10 - (sum1 + sum2 * 3) % 10;
System.out.println("奇數(shù)位的和:" + sum1 + "\n偶數(shù)位的和:" + sum2 + "\n得到的校驗碼:" + checkCode);
//其實應(yīng)該分個字符串放進(jìn)數(shù)組便利
/*
* 輸入12位條碼
* 690123456789
690123456789
奇數(shù)位的和:26
偶數(shù)位的和:34
得到的校驗碼:2
輸入12位條碼
692223361219
692223361219
奇數(shù)位的和:15
偶數(shù)位的和:31
得到的校驗碼:2
* */
```
-
循環(huán)結(jié)構(gòu):
//What is the result when you compile and run the following code? public class Test{ public void method(){ for(int i = 0; i < 3; i++) { System.out.print(i); } System.out.print(i); } }
A. 0122 B. 0123 C. compile error D. none of these
答案:C
-
打印1-100之間13的倍數(shù),使用for循環(huán)
static void test2() { for (int i = 1; i < 101; i++) { if (i % 13 == 0) { System.out.println(i); } } //共需執(zhí)行100 * 100 = 10000次 for (int i = 1; i <= 101 / 13; i++) { System.out.println(13 * i); } //只需執(zhí)行101/13次环础,遠(yuǎn)遠(yuǎn)小于第一種 //13//26//39//52//65//78//91 }
-
使用雙重循環(huán)打印20 * 8的矩形囚似,使用for循環(huán)實現(xiàn)
static void test3() { for (int i = 0; i < 8; i++) { String string = ""; for (int j = 0; j < 20; j++) { string = string + "*"; } System.out.println(string);//默認(rèn)換行 } }
用for循環(huán)計算1000以內(nèi)奇數(shù)的和
1)輸入長和寬,輸出長方形线得,如:輸入4和3, 將輸出如下圖形
2)輸入高度,輸出直角三角形徐伐。如:輸入4, 將輸出如下圖形
3)輸入高度贯钩,輸出倒直角三角形。如:輸入4, 將輸出如下圖形
打印九九乘法表
-
3000米長的繩子办素,每天減一半角雷。問多少天這個繩子會小于5米?不考慮小數(shù)性穿。
public static void main(String[] args) { int day = 0; for (int x = 3000; x >= 5; x /= 2) { day++; } System.out.println("day=" + day); /* * 方法二: * day = 0; * for(int x=3000; x>=5; day++) { * x = x/2; * } * System.out.println(day); */ }
打印1-100之間13的倍數(shù)勺三,使用continue語句
-
混合結(jié)構(gòu)練習(xí)
寫出結(jié)果public class Demo{ public static void main(String []args){ int i = 0, j = 5; tp: for (;;){ i++; for(;;){ if(i > j--) break tp; } } System.out.println("i = " + i + ", j = "+ j); //i=1,j=-1; } }
輸出從1到100之間所有不能被3整除的數(shù);并輸出這些整數(shù)的和
-
輸入兩個正整數(shù)m和n,求其最大公約數(shù)和最小公倍數(shù)
int m = 12, n = 28; //獲取m和n的較大值 int max = (m > n)? m : n; //獲取m和n的較小值 int min = (m < n)? m : n; //求m和n的最大公約數(shù) for(int i = min;i >= 1;i--){ if( m % i == 0 && n % i == 0){ System.out.println("m和n的最大公約數(shù)是:" + i); break; } } //求m和n的最小公倍數(shù) for(int i = max;i <= m * n;i++){ if( i % m == 0 && i % n == 0){ System.out.println("m和n的最小公倍數(shù)是:" + i); break; } }
-
根據(jù)指定月份需曾,打印該月份所屬的季節(jié)
分別使用if-else if-else語句和switch-case語句:
3,4,5 春季 6,7,8 夏季 9,10,11 秋季 12, 1, 2 冬季[answer] if(x==3 || x==4 || x==5) System.out.println(x+"春季"); else if(x==6 || x==7 || x==8) System.out.println(x+"夏季"); else if(x==9 || x==10 || x==11) System.out.println(x+"秋季"); else if(x==12 || x==1 || x==2) System.out.println(x+"冬季"); else System.out.println(x+"月份不存在"); [第二種] if(x>12 || x<1) System.out.println(x+"月份不存在"); else if(x>=3 && x<=5) System.out.println(x+"春季"); else if(x>=6 && x<=8) System.out.println(x+"夏季"); else if(x>=9 && x<=11) System.out.println(x+"秋季"); else System.out.println(x+"冬季"); [第3種] public static void main(String[] args) { int x = 4; switch(x){ case 3: case 4: case 5: System.out.println(x+"春季"); break; case 6: case 7: case 8: System.out.println(x+"夏季"); break; case 9: case 10: case 11: System.out.println(x+"秋季"); break; case 12: case 1: case 2: System.out.println(x+"冬季"); break; default: System.out.println("nono"); } }
-
已知學(xué)生成績以100分為滿分吗坚,共分5個等級:A,B,C,D,E。90~100為等級A呆万,80~89為等級B商源,70~79為等級C,60~69為等級D谋减,0~59為等級E牡彻。
要求定義一個成績變量,當(dāng)成績變化時出爹,可直接知道該成績對應(yīng)的等級庄吼。 例如:當(dāng)成績?yōu)?00時缎除,該學(xué)生的等級時A。 class LevelDemo{ //定義一功能总寻,通過給定分?jǐn)?shù)器罐,獲取該分?jǐn)?shù)對應(yīng)的等級。 public static void main(String[] args){ int num = 89; if(num>=90 && num<=100) System.out.println("level = A"); else if(num>=80 && num<=89) System.out.println("level = B"); else if(num>=70 && num<=79) System.out.println("level = C"); else if(num>=60 && num<=69) System.out.println("level = D"); else System.out.println("level = E"); } }
-
1)打印1~100之間 6的倍數(shù)的個數(shù);
2)求出1~100之間废菱,既是3又是7的倍數(shù)的自然數(shù)出現(xiàn)的次數(shù)技矮?public static void main(String[] args) { int count1 = 0,count2 = 0; for (int x = 1; x <= 100; x++) { if (x % 6 == 0){ count1++; } if(x % 3 == 0 && x % 7 == 0){ count2++; } } System.out.println("count1=" + count1); System.out.println("count2=" + count2); }
-
求調(diào)和級數(shù)中從第多少項開始和的值大于10
調(diào)和級數(shù)的第n項形式為:1+1/2+1/3+…+1/npublic static void main(String[] args) { double sum = 0.0; int i = 1; while (true) { sum += 1.0 / i; if (sum > 10) { break; } i++; } System.out.println(i); }
-
打印如下的圖形
* * * * * * * * * * * * * * * * * * * * * * * * * for (int i = 0; i < 7; i++) { if (i < 4) { for (int j = 0; j < 2 * i + 1; j++) { System.out.print("* "); } System.out.println(); } else { for (int k = 0; k < 13 - 2 * i; k++) { System.out.print("* "); } System.out.println(); } }
-
【拓展】打印如下的圖形
* * * * * * * * * * * * * * * * * * * * * * * * * // 上半部分 for (int i = 0; i < 5; i++) { // 輸出“-” for (int j = 0; j < 4 - i; j++) { System.out.print(" "); } // 輸出“* ” for (int k = 0; k < i + 1; k++) { System.out.print("* "); } System.out.println(); } // 下半部分 for (int i = 0; i < 4; i++) { for (int j = 0; j < i + 1; j++) { System.out.print(" "); } for (int k = 0; k < 4 - i; k++) { System.out.print("* "); } System.out.println(); } //解法2 static void test6() { Scanner scan = new Scanner(System.in); System.out.println("請輸入菱形的規(guī)模,僅限與奇數(shù)"); int startsCount = 0; if (scan.hasNextInt()) { startsCount = scan.nextInt(); } if (startsCount % 2 == 0) { System.out.println("非奇數(shù)"); return; } //打印目標(biāo) int a = startsCount / 2 + 1; for (int i = 0; i < startsCount; i++) { String currentString = ""; if (i < a) {//上部 for (int j = 0; j < startsCount / 2 - i; j++) { currentString += " "; } for (int j = 0; j <= i; j++) { currentString += "* "; } } else {//下部 /* * 中間星星的總數(shù) = 總數(shù)/2 +1殊轴; * 空格數(shù) = 中間星星數(shù) - (行數(shù)相對于中間的相對值)衰倦; * */ for (int j = 0; j < i-a+1; j++) { currentString += " "; } for (int j = a - (i-a+1); j > 0; j--) { currentString += "* "; } } System.out.println(currentString); } }
-
拓展:打印如下的圖形
********** **** **** *** *** ** ** * * ** ** *** *** **** **** ********** //code static void test7() { int rows = 9; for (int i = 0; i < rows; i++) { String starsString = ""; if (i <= rows/2) { //上部 //分左右 for (int j = 0; j < rows+1; j++) { //總數(shù) rows+1 //星星數(shù) 2*i if (j >= (rows+1)/2-i && j< (rows+1)/2+i) { starsString += " "; }else { starsString += "*"; } } } else { //下部 for (int j = 0; j < rows+1; j++) { if (j >= (i-rows/2)+1 && j < (rows+1)-((i-rows/2)+1)) { starsString += " "; } else { starsString += "*"; } } } System.out.println(starsString); } }
-
編寫程序,打印100-200之間的質(zhì)數(shù)
for (int i = 100; i <= 200; i++) { int count = 0;//記錄當(dāng)前i的因數(shù)個數(shù) for (int j = 1; j <= i; j++) { if (i % j == 0) { count ++; if (count > 2) { continue; } } } if (count == 2) { System.out.println("素數(shù)" + i); } }
-
一個數(shù)如果恰好等于它的因子之和旁理,這個數(shù)就稱為"完數(shù)"
(因子:除去這個數(shù)本身正的約數(shù))
例如6=1+2+3.編程 找出1000以內(nèi)的所有完數(shù)public class WanShu { //方法2 自寫 for (int i = 1; i <= 1000; i++) { int sum = 0; for (int j = 1; j < i; j++) { if (i % j == 0) { sum += j; } } if (sum == i) { System.out.println(i); } } //方法1 Demo static int count; public static void main(String[] args) { for (int i = 1; i <= 1000; i++) { int factor = 0; for (int j = 1; j < i; j++) { if (i % j == 0) factor += j; } if (factor == i) { System.out.println(i); count++; } } System.out.println("1-1000之間的完數(shù)個數(shù)為:" + count); } }
-
??(數(shù)組暫時沒學(xué))寫一個程序樊零,找出4位數(shù)的所有吸血鬼的數(shù)字
例如:1260=21*60
1827=21*87public class Test1 { public static void main(String[] args) { for (int num = 1001; num < 10000; num++) { math(num); } } public static void math(int num) { int[] temp1 = new int[2]; int[] temp2 = new int[2]; int a = num / 1000; int b = num / 100 % 10; int c = num / 10 % 10; int d = num % 10; int[] data = { a, b, c, d }; for (int i = 0; i < data.length; i++) { for (int j = 0; j < data.length; j++) { if (i == j) { continue; } temp1[0] = data[i]; temp1[1] = data[j]; for (int m = 0; m < data.length; m++) { if (m != i && m != j) { temp2[0] = data[m]; for (int n = 0; n < data.length; n++) { if (n != i && n != j && n != m) { temp2[1] = data[n]; multi(data, temp1, temp2); } } } } } } } public static int toInt(int[] temp) { int m = 0; int[] temp1 = new int[temp.length]; for (int i = 0; i < temp.length; i++) { temp1[i] = temp[i] * (int) Math.pow(10, temp.length - 1 - i); } for (int i = 0; i < temp1.length; i++) { m += temp1[i]; } return m; } public static void multi(int[] temp, int[] temp1, int[] temp2) { int i = toInt(temp1); int j = toInt(temp2); int k = toInt(temp); if (k == i * j) { System.out.println(k + "=" + i + "*" + j); } } }
-
輸出所有的水仙花數(shù)。所謂水仙花數(shù)是指一個3位數(shù)孽文,其各個位上數(shù)字立方和等于其本身驻襟。
例如: 153 = 1*1*1 + 3*3*3 + 5*5*5 class ShuiXianHua { public static void main(String[] args) { for (int i = 100; i < 1000; i++) {// 實現(xiàn)所有的三位數(shù)的一個遍歷 int j1 = 0; int j2 = 0; int j3 = 0; j1 = i / 100;// 百位 j2 = (i - 100 * j1) / 10;// 十位 j3 = i - 100 * j1 - 10 * j2;// 個位 if (i == j1 * j1 * j1 + j2 * j2 * j2 + j3 * j3 * j3) { System.out.println("此數(shù)值為滿足條件的水仙花數(shù):" + i); } } } }
山上有一口缸可以裝50升水,現(xiàn)在有15升水芋哭。老和尚叫小和尚下山挑水沉衣,每次可以挑5升。問:小和尚要挑幾次水才可以把水缸挑滿减牺?通過編程解決這個問題豌习。
提示:
(1) 用整型變量water表示水缸里的水“int water = 15;”。
(2) 用整型變量l表示小和尚下山挑水的次數(shù)“int l = 0;”拔疚。
(3) 分析循環(huán)條件(水少于50升)肥隆,循環(huán)操作(水增加5升,挑水次數(shù)增加1)稚失。
(4) 套用while循環(huán)(或do-while循環(huán))寫出代碼栋艳。實現(xiàn)判斷一個4位整數(shù),統(tǒng)計出此整數(shù)里面包含多少個偶數(shù)句各,多少個奇數(shù)的功能
開發(fā)一款軟件吸占,根據(jù)公式(身高-108)*2=體重,可以有10斤左右的浮動诫钓。來觀察測試者體重是否合適旬昭。
有3個整數(shù),給出提示信息:
能否創(chuàng)建三角形菌湃;兩邊之和大于第三邊 三個條件都要寫
如果能構(gòu)建三角形问拘,提示是直角三角形還是等邊三角形等腰三角形還是普通三角形;
最后輸出三角形面積;在JAVA中骤坐,如何跳出當(dāng)前的多重嵌套循環(huán)绪杏?
答:用break; return 方法。