java流程控制(使用者互動、順序結構、迴圈結構)
Java流程控制
使用者互動Scanner
-
Scanner s = new Scanner(System.in);
-
Scanner 類的next()和nextLine()方法獲取輸入的字串的區別:
-
next沒有拼接字串的功能,如這裡不能寫hello world,可以寫helloworld
public static void main(String[] args) { //建立一個掃描器,使用者鍵盤接收資料 Scanner scanner = new Scanner(System.in); System.out.println("使用next方式接收:"); if (scanner.hasNext()){ //獲取鍵盤輸入的資料,將資料傳給str String str = scanner.next(); System.out.println("輸入的內容為:"+str); } //關閉 scanner.close(); }
-
nextLine可以自動拼接字串,下面的程式碼不用if語句,因為在實際使用中,可以直接使用,不需要if來判斷,科省略
public static void main(String[] args) { //建立一個掃描器,獲取鍵盤輸入的值 Scanner scanner = new Scanner(System.in); //將獲取到的資料傳給str String str = scanner.nextLine(); //輸出 System.out.println("你輸入的資料是:"+str); scanner.close(); }
-
例子
public static void main(String[] args) { //輸入多個數字,顯示當前輸入了多少個數字、輸入數字的總和以及平均數 Scanner scanner = new Scanner(System.in); //總數 double sum= 0 ; //輸入數字個數 int i = 0; while (scanner.hasNextDouble()){ double x = scanner.nextDouble(); i =i+1; sum = sum+x; System.out.println("你當前輸入的第:"+i+"個數據,當前和結果為:"+sum); } System.out.println(i+"個數的和是:"+sum); System.out.println(i+"個數的平均值是:"+(sum/i)); scanner.close(); }
10
你當前輸入的第:1個數據,當前和結果為:10.0
20
你當前輸入的第:2個數據,當前和結果為:30.0
30
你當前輸入的第:3個數據,當前和結果為:60.0
w
3個數的和是:60.0
3個數的平均值是:20.0
-
順序結構
- java的基本結構就是順序結構,除非特別指明,否則就是一句一句的執行
- 是任何演算法都離不開的一個基本結構
選擇結構
-
if單選擇結構:
public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("請輸入金額,單位萬"); String money = scanner.nextLine(); if (money.equals("100")){ System.out.println("支付成功,支付金額為"+money+"萬,收購成功"); }else{ System.out.println("支付失敗,支付金額為"+money+"萬,自己做開發"); } scanner.close(); }
-
if雙選擇結構:
public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("請輸入成績"); int score = scanner.nextInt(); if (score>60){ System.out.println("及格"); }else{ System.out.println("不及格"); } scanner.close(); }
-
多選擇結構:一旦其中一個else if語句檢測為true,則其他的else if以及else語句都將跳過執行
Scanner scanner = new Scanner(System.in); System.out.println("請輸入成績"); int score = scanner.nextInt(); if (score==100){ System.out.println("恭喜滿分"); }else if (score>90){ System.out.println("A級"); }else if (score>80) { System.out.println("B級"); }else if (score>70) { System.out.println("C級"); }else if (score>60) { System.out.println("D級"); }else { System.out.println("不及格"); } scanner.close(); }
-
巢狀if結構:
-
switch多選擇結構:jdk7以後,可以接字串,比如grade ='英俊',case'英俊'
char grade = 'A'; switch (grade){ case 'A': System.out.println("優秀"); break; case 'B': System.out.println("良好"); break; case 'C': System.out.println("及格"); break; case 'D': System.out.println("掛科"); }
迴圈結構
-
while迴圈:只要布林表示式為true,迴圈就會一直持續下去,儘量避免死迴圈,例子(計算1-100數相加的和)
public static void main(String[] args) { int i = 0; int sum = 0; while (i<=100){ sum = sum+i; i++; } System.out.println(sum); }
-
do...while迴圈:
public static void main(String[] args) { int a = 0; while (a<0){ System.out.println(a);//不輸出結果 a++; } System.out.println("--------------------------"); do { System.out.println(a);//可以輸出a=0,以為do while ,至少會執行一次,不管迴圈成立與否 a++; }while (a<0); }
-
for迴圈:是一種最有效、最靈活的迴圈結構。
public static void main(String[] args) { int a; for (a=0;a<100;a++){ System.out.println(a); } System.out.println("for迴圈結束"); }
-
for迴圈巢狀實列:(列印99乘法表)
public static void main(String[] args) { for (int i = 1; i <=9; i++) { for (int j = 1; j <= i; j++) { System.out.print(j+"*"+i+"="+i*j+"\t"); } System.out.println(); } }
-
增強for迴圈
public static void main(String[] args) { int[] number = {10,20,30,40,50}; for (int i = 0;i<5;i++){ System.out.println(number[i]); } System.out.println("+++++++++++++++++++++"); for (int x:number){ System.out.println(x);//下面這個是增強for迴圈,遍歷了一個數組,結果和上面的for迴圈一樣 } }
break continue
-
break 用於強制退出迴圈,不執行迴圈中剩餘的語句。
-
continue用於終止某次迴圈過程,即跳過迴圈體中尚未執行的語句,接著執行下一次迴圈判定。
-
goto關鍵字:指從迴圈中跳出到另一個迴圈之中來,如下例中,通過給迴圈命名,然後在continue你想跳到的迴圈名。
//練習:輸出101—151中的所有質數,質數指的是,在大於一的自然數中只存在1和他本身為因素的自然數。 public static void main(String[] args) { int count = 0; out: for (int i=101;i<151;i++){ for (int j=2;j<i/2;j++){ if (i%j == 0){ continue out; } } System.out.println(i+""); } }
for迴圈例子
-
列印三角形:
public static void main(String[] args) { //列印三角形 for (int i = 1; i <=5; i++) { for (int j=5;j>=i;j-- ){ System.out.print(" "); } for (int j=1;j<=i;j++ ){ System.out.print("*"); } for (int j = 1;j<i;j++){ System.out.print("*"); } System.out.println(); } }
![image-20210720154656762](C:\Users\MKZ Lenovo\AppData\Roaming\Typora\typora-user-images\image-20210720154656762.png)