Java基礎第四天
1.1分支結構2:switch語句
1.根據變量的值,選擇相應的case去判斷,一旦滿足case條件,就執行case的相應語句。如果沒有break或者已經
到結尾的話,會繼續執行其下的case語句。
2.default:是可選的,而且位置是靈活的。
3.變量可以是哪些類型?char byte short int 枚舉 String(jdk1.7)
4.case 條件:其中條件只能是值,不能是取值範圍!
switch語句應用舉例
import java.util.Scanner; class TestSwitch { public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.println("請輸入季節:"); String season = s.next(); switch(season){ case "Spring": System.out.println("春天"); break; case "Summer": System.out.println("夏天"); break; case "Autum": System.out.println("秋天"); break; case "Winter": System.out.println("冬天"); break; default : System.out.println("輸入有誤"); break; } } }
練習:
根據用於指定月份,打印該月份所屬的季節。
3,4,5 春季 6,7,8 夏季 9,10,11 秋季 12, 1, 2 冬季
import java.util.Scanner; class TestSwitch2 { public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.println("請輸入月份:"); int mounth = s.nextInt(); if(mounth > 12 || mounth < 1){ System.out.println("輸入有誤!"); }else{ switch(mounth/3){ case 1: System.out.println("春季"); break; case 2: System.out.println("夏季"); break; case 3: System.out.println("秋季"); break; default: System.out.println("冬季"); break; } } } }
import java.util.Scanner; class TestSwitch1 { public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.println("請輸入月份:"); int mounth = s.nextInt(); switch(mounth){ case 12: case 1: case 2: System.out.println("冬季!"); break; case 3: case 4: case 5: System.out.println("春季!"); break; case 6: case 7: case 8: System.out.println("夏季!"); break; case 9: case 10: case 11: System.out.println("秋季!"); break; default: System.out.println("輸入有誤!"); break; } } }
編寫程序:從鍵盤上輸入2016年的“month”和“day”,要求通過程序輸出輸入的日期爲2014年的第幾天。
import java.util.Scanner; class TestSwitch3 { public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.println("請輸入月份:"); int mounth = s.nextInt(); System.out.println("請輸入日期"); int day = s.nextInt(); int sum = 0; switch(mounth){ case 12: sum += 30; case 11: sum += 31; case 10: sum += 30; case 9: sum += 31; case 8: sum += 31; case 7: sum += 30; case 6: sum += 31; case 5: sum += 30; case 4: sum += 31; case 3: sum += 28; case 2: sum += 31; case 1: sum += day; } System.out.println(sum); } }
1.2循環結構
循環語句功能
在某些條件滿足的情況下,反覆執行特定代碼的功能
循環語句的四個組成部分
初始化部分(init_statement)
循環條件部分(test_exp)
循環體部分(body_statement)
迭代部分(alter_statement)
循環語句分類
for 循環
while 循環
do/while 循環
for 循環
案例:
class TestFor { public static void main(String[] args) { for (int i = 0; i < 100 ;i++){ System.out.println("Hello World!" + i); } } }
題目:輸出100以內的所有偶數及所有偶數的和(累加的思想)及偶數的個數
class TestFor1 { public static void main(String[] args) { int sum = 0; int count = 0; for(int i = 0; i <= 100; i++){ if(i % 2 == 0){ System.out.println(i); sum += i; count++; } } System.out.println(sum); System.out.println(count); } }
小測試:
編寫程序FooBizBaz.java,從1循環到150並在每行打印一個值,另外在每個3的倍數行上打印出“foo”,在每個5的倍數行上打印“biz”,在每個7的倍數行上打印輸出“baz”。
class FooBizBaz { public static void main(String[] args) { for(int i = 1;i <= 150;i++){ System.out.print(i + " "); if(i % 3 ==0){ System.out.print("foo "); } if(i % 5 == 0){ System.out.print("biz "); } if(i % 7 == 0){ System.out.print("baz "); } System.out.println();//換行 } } }
練習:輸出所有的水仙花數,所謂水仙花數是指一個3
位數,其各個位上數字立方和等於其本身。
例如: 153 = 1*1*1 + 3*3*3 + 5*5*5
class TestFor2 { public static void main(String[] args) { for(int i = 100 ; i < 1000 ; i++){ int i1 = i / 100; int i2 = (i - i1*100)/10; int i3 = i - i1*100 - i2*10;//int i3 = i % 10; if(i == i1*i1*i1 + i2*i2*i2 + i3*i3*i3 ){ System.out.println(i); } } } }
While循環
語法格式
[初始化語句]
while( 布爾值測試表達式){
語句或語句塊;
[更改語句;]
}
While案例:100以內的偶數的輸出,及其和
class TestWhile{ public static void main(String[] args) { int i = 1; int sum = 0; while(i <= 100){ if(i % 2 == 0){ System.out.println(i); sum += i; } i++; } System.out.println(sum); System.out.println(i); } }
do-while 循環語句
語法格式
[初始化語句]
do{
語句或語句塊;
[更改語句;]
}while(布爾值測試表達式);
class TestDoWhile { public static void main(String[] args) { int i = 1; do{ if(i % 2 == 0){ System.out.println(i); } i++; }while(i <= 100); //do-while與while的區別 int j = 10; do{ System.out.println(j); j++; }while(j < 10); while(j < 10){ System.out.println(j); j++; } } }
案例:從鍵盤讀入個數不確定的整數,並判斷讀入的正數和負數的個數,輸入爲0時結束程序。
補充:
最簡單無限循環格式:while(true) , for(;;),無限循環存在的原因是並不知道循環多少次,需要根據某些條件,來控制循環。
import java.util.Scanner; class TestWhileTure { public static void main(String[] args) { //System.out.println("Hello World!"); Scanner s = new Scanner(System.in); int a = 0; int b = 0; /*for(;;){ System.out.println("請輸入一個整數"); int num = s.nextInt(); if(num > 0){ a++; }else if(num < 0){ b++; }else{ break; } } */ while(true){ System.out.println("請輸入一個整數"); int num = s.nextInt(); if(num > 0){ a++; }else if(num < 0){ b++; }else{ break; } } System.out.println("正數有"+a+"個"); System.out.println("負數有"+b+"個"); } }
1.3嵌套循環
將一個循環放在另一個循環體內,就形成了嵌套循環。其中,for ,while ,do…while均可以作爲外層循環和內層循環。
案例:
/*
*****
*****
*****
*****
*/
class TestForFor { public static void main(String[] args) { /*for(int i = 0 ; i < 4 ; i++){//行循環 for(int j = 0 ; j < 5 ; j++){//列循環 System.out.print("*"); } System.out.println(); } } }
/*
*
**
***
****
*/
class TestForFor { public static void main(String[] args) { /*for(int i = 0 ; i < 4 ; i++){//行循環 for(int j = 0 ; j < i+1 ; j++){//列循環 System.out.print("*"); } System.out.println(); } } }
/*
*****
****
***
**
*
*/
class TestForFor { public static void main(String[] args) { /*for(int i = 0 ; i < 5 ; i++){//行循環 for(int j = 0 ; j < 5-i ; j++){//列循環 System.out.print("*"); } System.out.println(); } } }
/*
*
**
***
****
*****
****
***
**
*
*/
class TestForFor { public static void main(String[] args) { for(int i = 0;i < 5; i++){ for(int j = 0;j < i+1; j++){ System.out.print("*"); } System.out.println(); } for(int i = 0; i < 4; i++){ for(int j = 0; j < 4-i; j++){ System.out.print("*"); } System.out.println(); } } }
打印如下圖形
----*
---* *
--* * *
-* * * *
* * * * *
-* * * *
--* * *
---* *
----*
class TestForFor { public static void main(String[] args) { for(int i = 0;i < 4;i++){ for(int k = 0;k < 4-i;k++){ System.out.print(" "); } for(int j = 0;j < i+1;j++){ System.out.print("* "); } System.out.println(); } for(int i = 0;i < 5;i++){ for(int k = 0;k < i;k++){ System.out.print(" "); } for(int j = 0;j < 5-i; j++){ System.out.print("* "); } System.out.println(); } } }
打印九九乘法表:
class TestJiuJiu { public static void main(String[] args) { for(int i = 1;i < 10;i++){ for(int j = 1;j < i+1;j++){ System.out.print(i+"*"+j+"="+i*j+"\t"); } System.out.println(); } } }
1.4特殊流程控制語句1
break 語句
break語句用於終止某個語句塊的執行
{ ……
break;
……
}
continue 語句
continue語句用於跳過某個循環語句塊的一次執行
continue語句出現在多層嵌套的循環語句體中時,可以通過標籤指明要跳過的是哪一層循環
break只能用於switch語句和循環語句中。
continue 只能用於循環語句中。
二者功能類似,但continue是終止本次循環,break是終止本層循環。
break、continue之後不能有其他的語句,因爲程序永遠不會執行其後的語句。
標號語句必須緊接在循環的頭部。標號語句不能用在非循環語句的前面。