需求:需要打印一行字符串"hello gzitcast",100次
就需要將該語句打印100遍System.out.println("hello gzitcast");
那么如何解決該問題?
Java提供個一個稱之為循環的結構,用來控制一個操作的重復執行。
int count = 0; while (count < 100) { System.out.println("hello gzitcast"); count++; } System.out.println("over");
變量count初始化值為0,循環檢查count<100 是否為true,如果為true執行循環體(while后{}之間的語句),輸出"hello gzitcast"語句,然后count自增一,重復循環,直到count是100時,也就是count<100為false時,循環停止。執行循環之后的下一條語句。
Java提供了三種類型的循環語句:while循環,do-while循環和for循環。
1、while語句格式: while(條件表達式) { 執行語句; }
定義需求: 想要打印5次helloworld
public static void main(String[] args) { System.out.println("hello world"); System.out.println("hello world"); System.out.println("hello world"); System.out.println("hello world"); System.out.println("hello world"); }
2、
public static void main(String[] args) { int x = 0; while (x < 5) { System.out.println("hello java "); } }
如果是在dos里編譯和運行,是不會停止,除非系統死機。需要ctrl+c來結束
這就是真循環或者死循環。因為x<5 永遠為真。
public static void main(String[] args) { int x = 0; while (x < 5) { System.out.println("hello java "); x++; } }
讓x自增,那么就會有不滿足條件的時候。循環就會結束。
練習:想要打印出1-100之間的奇數
public static void main(String[] args) { int x = 1; while (x < 100) { System.out.println(x); x = x + 2; } }
public static void main(String[] args){ int x=1; while(x<100){ if(x%2!=0){ System.out.print(x); } x++; } System.out.println(); }
練習2:計算1+2+3+4+5+6+7+8+9 的值
int sum = 0; int i = 1; while (i < 10) { sum = sum + i; i++; } system.out.println(sum);
注意:要精確控制循環的次數。常犯錯誤是是循環多執行一次或者少執行一次。
例如會執行101次,想要執行100次,要么是count初始值為1,然后count<=100
要么是count初始值為0,coung<100
int count = 0; while (count <=100) { System.out.println("hello gzitcast"); count++; } System.out.println("over");
猜數字游戲:
編寫程序隨即生成一個0-100之間的隨機數。程序提示用戶輸入一個數字,不停猜測,直到猜對為止。最后輸出猜測的數字,和猜測的次數。並且如果沒有猜中要提示用戶輸入的值是大了還是小了。
思考:
如何生成1-100之間隨機數?
(int)(Math.random()*100)+1;
如何提示用戶輸入數字,
Scanner sc=new Scanner(System.in);
int guessNum = sc.nextInt();
需要將隨機數和用戶輸入的數字進行比較。
猜一次
Scanner sc = new Scanner(System.in); int num = (int)(Math.random()*100)+1; System.out.println("請輸入0-100之間整數"); int guessNum = sc.nextInt(); if (guessNum == num) { System.out.println("中啦"); } else if (guessNum < num) { System.out.println("小啦"); } else { System.out.println("大了"); }
這個程序只能才一次,如何讓用戶重復輸入直到猜對?
可以使用while循環
public static void main(String[] args) { int num = (int)(Math.random()*100)+1; Scanner sc = new Scanner(System.in); while (true) { System.out.println("請輸入1-100之間整數"); int guessNum = sc.nextInt(); if (guessNum == num) { System.out.println("中啦"); } else if (guessNum < num) { System.out.println("小啦"); } else { System.out.println("大了"); } } }
該方案發現了問題,雖然實現了讓用戶不停的輸入,但是即使猜中了程序也不會停止。
那么就需要控制循環次數了。也就是while() 括號中的條件表達式。當用戶猜測的數和系統生成的數字不相等時,就需要繼續循環。
int num = (int)(Math.random()*100)+1; Scanner sc = new Scanner(System.in); int guessNum = -1; while (guessNum != num) { System.out.println("請輸入1-100之間整數"); guessNum = sc.nextInt(); if (guessNum == num) { System.out.println("中啦"); } else if (guessNum < num) { System.out.println("小啦"); } else { System.out.println("大了"); } }
為什么將guessNum初始化值為-1?因為如果初始化為0到100之間程序會出錯,因為可能是要猜的數。
1:首先程序生成了一個隨機數
2:用戶輸入一個數字
3:循環檢查用戶數字和隨機數是否相同,知道相同位置,循環結束
do while 語句
do while語句格式:
do { 執行語句; }while(條件表達式); do while特點是條件無論是否滿足, 循環體至少被執行一次。
public static void main(String[] args) { int x = 0, y = 0; do { System.out.println(x); x++; } while (x < 0); // do while do會先執行一次,不管是否滿足循環條件。 while (y < 0) { System.out.println(y); y++; } }
while:先判斷條件,只有條件滿足才執行循環體。
do while: 先執行循環體,再判斷條件,條件滿足,再繼續執行循環體。
簡單一句話:do while:無論條件是否滿足,循環體至少執行一次。
注意一個細節do while 后面的分號;
案例:改寫猜數字游戲
public static void main(String[] args) { // 記錄用戶輸入的數字 int guess = -1; // 記錄用戶輸入次數 int count = 0; // 生成1-100之間隨機數 int num = (int) (int)(Math.random()*100)+1; Scanner sc = new Scanner(System.in); // 循環猜數字 do { System.out.println("請輸入1-100之間的數字"); guess = sc.nextInt(); if (guess > num) { System.out.println("哥們,太大了"); } else if (guess < num) { System.out.println("哥們,太小了"); } else { System.out.println("恭喜,中啦"); } count++; } while (num != guess); System.out.println("你猜測的數字是:" + num + "猜測了" + count + "次"); }
案例:計算器
系統自動生成2個隨機數用於參與運算。
系統生成0-4之間的隨機數,表示加減乘除取模運算。
使用switch 進行匹配
class Couter { public static void main(String[] args) throws InterruptedException { // 生成隨機數Math.random()生成0-1值,不包含0和1, //乘以10得到0和10之間的數(double類型),不包含0和10 //強轉為int,並加1得到1和10之間的數,包含1和10 int x = (int)(Math.random()*10)+1; int y = (int)(Math.random()*10)+1; System.out.println(x); System.out.println(y); // 創建0-4隨機數 0 1 2 3 4 各表示加減乘除取模 int z = (int) (int)(Math.random()*5); System.out.println(z); switch (z) { case 0: System.out.println(x + "+" + y + "=?"); System.out.println("哥們快猜。。。。"); Thread.sleep(2000); System.out.println(x + "+" + y + "=" + (x + y)); break; case 1: System.out.println(x + "-" + y + "=?"); System.out.println("哥們快猜。。。。"); Thread.sleep(2000); System.out.println(x + "-" + y + "=" + (x - y)); break; case 2: System.out.println(x + "*" + y + "=?"); System.out.println("哥們快猜。。。。"); Thread.sleep(2000); System.out.println(x + "*" + y + "=" + (x * y)); break; case 3: System.out.println(x + "/" + y + "=?"); System.out.println("哥們快猜。。。。"); Thread.sleep(2000); System.out.println(x + "/" + y + "=" + (x / y)); break; case 4: System.out.println(x + "%" + y + "=?"); System.out.println("哥們快猜。。。。"); Thread.sleep(2000); System.out.println(x + "%" + y + "=" + (x % y)); break; }
}
}
計算器2:上述中只能計算一次。可以使用whileu循環來不停計算。
程序生成了3個隨機數,前兩個數參與運算,第三個數用於匹配運算符。要注意除數為0的情況。
int x = (int)(Math.random()*10)+1;
Math.random() 生成0-1之間的數字,double類型
Math.random()*10 就是0-9之間的數,是double類型
(int)(Math.random()*10)將double類型強轉成int類型,去掉小數點,便於計算。
(int)(Math.random()*10)+1,生成了1到10之間隨機數。
int z = (int) (int)(Math.random()*5);
生成0-4之間的數字,可以用0表示加,1表示減,2表示乘,3表示除,4表示取模
為了減慢程序,使用了Thread.sleep(2000); 讓程序等待一會。
