java基礎 ----- 循環結構
阿新 • • 發佈:2019-04-12
ner exti 循環 png class equals next() ava NPU
循環的結構特點 : 循環條件 循環操作
----- while 循環
來個小例子,實現打印50 份shij
1、確定循環條件和循環操作
2、套用while語法寫出代碼
3、檢查循環能否退出
1 public class WhileDemo { 2 3 /* 4 * 如何使用while循環 5 */ 6 public static void main(String[] args) { 7 int count = 1; 8 while(count <= 50){9 System.out.println("打印第" +count+ "份試卷"); 10 count++; 11 } 12 } 13 }
復雜一點 通過輸入的進行判斷要不要進行循環
1 import java.util.Scanner; 2 3 public class WhileDemo { 4 5 /* 6 * 如何使用while循環 7 */ 8 public static void main(String[] args) {9 String answer; //標識是否合格 10 Scanner input = new Scanner(System.in); 11 System.out.print("合格了嗎?(y/n):"); 12 answer = input.next(); 13 while(!"y".equals(answer)){ 14 System.out.println("上午閱讀教材!"); 15 System.out.println("下午上機編程!\n");16 System.out.print("合格了嗎?(y/n):"); 17 answer = input.next(); 18 } 19 System.out.println("完成學習任務!"); 20 } 21 }
------------- do- while 循環
//經過幾天學習,要讓其先進行編寫在進行,判斷要不要進行編寫 import java.util.Scanner; public class DoWhileDemo { /* * 如何使用do-while循環 */ public static void main(String[] args) { Scanner input = new Scanner(System.in); String answer = ""; //標識是否合格 do{ System.out.println("上機編寫程序!"); System.out.print("合格了嗎?(y/n)"); answer = input.next(); System.out.println(""); }while(!"y".equals(answer)); System.out.println("恭喜你通過了測試!"); } }
註意: while 與 do-while 循環的區別
--- for 循環
好處 : 比while循環更加簡潔
計算平均分
1 import java.util.*; 2 public class AverageScore{ 3 /** 4 * 統計80分以上學生比例 5 */ 6 public static void main(String[] args){ 7 int score; //每門課的成績 8 int sum = 0; //成績之和 9 double avg = 0.0; //平均分 10 Scanner input = new Scanner(System.in); 11 System.out.print("輸入學生姓名: "); 12 String name = input.next(); 13 for(int i = 0; i < 5; i++){ //循環5次錄入5門課成績 14 System.out.print("請輸入5門功課中第" + (i+1) + "門課的成績: "); 15 score = input.nextInt(); //錄入成績 16 sum = sum + score; //計算成績和 17 } 18 avg = (double)sum / 5; //計算平均分 19 System.out.println(name + "的平均分是:" + avg); 20 } 21 }
輸出加法表
1 import java.util.*; 2 public class SumTable { 3 /** 4 * 輸入整數,輸出其加法表 5 */ 6 public static void main(String[] args){ 7 int i, j; 8 Scanner input = new Scanner(System.in); 9 System.out.print("請輸入一個值: "); 10 int val = input.nextInt(); 11 System.out.println("根據這個值可以輸出以下加法表: "); 12 for(i = 0, j = val; i <= val; i++, j--){ 13 System.out.println(i + " + " + j + " = " + (i+j)); 14 } 15 } 16 }
for循環裏面的不可以少兩個分號啊
--------- 總結 三個循環的特點
-------- 區別
區別2: 執行順序
while循環:先判斷,在執行
do-while循環:先執行,在判斷
for循環: 先判斷,在執行
區別3: 適用情況
循環次數確定的情況通常使用for循環
循環次數不確定的情況通常使用while循環 在判斷是先執行在循環 還是先循環在執行
-- 使用三種方法計算100以內偶數和
1 public class EvenSum { 2 3 /* 4 * 計算100以內偶數之和 5 * 思考:還有沒有其他方式? 6 */ 7 public static void main(String[] args) { 8 //使用while 9 /*int sum = 0; //當前之和 10 int num = 2; //加數 11 while(num <= 100){ 12 sum = sum + num; //累加 13 num = num + 2; 14 } 15 System.out.println("100以內的偶數之和為:" + sum);*/ 16 17 //使用do-while 18 /*int sum = 0; //當前之和 19 int num = 2; //加數 20 do{ 21 sum = sum + num; //累加 22 num = num + 2; 23 }while(num <= 100); 24 System.out.println("100以內的偶數之和為:" + sum);*/ 25 26 //使用for 27 int sum = 0; //當前之和 28 for(int num=2;num<=100;num=num+2){ 29 sum = sum + num; //累加 30 } 31 System.out.println("100以內的偶數之和為:" + sum); 32 } 33 }
java基礎 ----- 循環結構