day06
一、for循環嵌套
需求1:打印以下圖形************for(int i = 0;i<3;i++){//控制行數for(int j = 0;j<4;j++){//控制列數System.out.print("*");}System.out.println();//換行}需求2:打印以下圖形* i=0** i=1*** i=2**** i=3***** i=4for(int i = 0;i<5;i++){for(int j = 0;j<=i;j++){System.out.print("*");}System.out.println();}需求3:打印以下圖形***************for(int i = 0;i<5;i++){for(int k = 0;k<4-i;k++){System.out.print(" ");}for(int j = 0;j<=i;j++){System.out.print("*");}System.out.println();}需求4:打印以下圖形***************for(int i = 0;i<5;i++){for(int j = 0;j<5-i;j++){System.out.print("*");}System.out.println();}需求5:打印以下圖形***************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();}需求6:打印以下圖形****************for(int i = 0;i<4;i++){for(int k = 0;k<3-i;k++){System.out.print(" ");}for(int j = 0;j<i*2+1;j++){System.out.print("*");}System.out.println();}需求7:打印以下圖形** ** ********for(int i = 0;i<4;i++){for(int k = 0;k<3-i;k++){System.out.print(" ");}for(int j = 0;j<i*2+1;j++){//第一行 || 最后一行 || 每行的第一列 || 每行的最后一列(以形成空心)if(i==0 || i==3 || j==0 || j==i*2){//最后一列j==i*2+1取不到要減1,亦或因為從0開始沒有取等,也正是沒有取等簡化表達式System.out.print("*");}else{System.out.print(" ");}}System.out.println();}需求8:打印以下圖形****************for(int i = 0;i<4;i++){for(int k = 0;k<i;k++){System.out.print(" ");}for(int j = 0;j<7-2*i;j++){System.out.print("*");}System.out.println();}需求9:打印以下圖形******** ** **for(int i = 0;i<4;i++){for(int k = 0;k<i;k++){System.out.print(" ");}for(int j = 0;j<7-2*i;j++){//第一行 || 最后一行 || 每行的第一列 || 每行的最后一列if(i==0 || i==3 || j==0 || j==7-2*i-1){//最后一列j==7-i*2取不到要減1,亦或因為從0開始沒有取等,也正是沒有取等簡化表達式System.out.print("*");}else{System.out.print(" ");}}System.out.println();}需求10:九九乘法表1x1=1 1x2=2 2x2=4 1x3=3 2x3=6 3x3=9 1x4=4 2x4=8 3x4=12 4x4=16 1x5=5 2x5=10 3x5=15 4x5=20 5x5=25 1x6=6 2x6=12 3x6=18 4x6=24 5x6=30 6x6=36 1x7=7 2x7=14 3x7=21 4x7=28 5x7=35 6x7=42 7x7=49 1x8=8 2x8=16 3x8=24 4x8=32 5x8=40 6x8=48 7x8=56 8x8=64 1x9=9 2x9=18 3x9=27 4x9=36 5x9=45 6x9=54 7x9=63 8x9=72 9x9=81for(int i = 1;i<=9;i++){//九九乘法表從1開始for(int j = 1;j<=i;j++){//不一定有九列System.out.print(j + "x" + i + "=" + (i*j) + "\t");//制表符對齊}System.out.println();}需求11:九九乘法表1x1=1 1x2=2 1x3=3 1x4=4 1x5=5 1x6=6 1x7=7 1x8=8 1x9=9 2x2=4 2x3=6 2x4=8 2x5=10 2x6=12 2x7=14 2x8=16 2x9=18 3x3=9 3x4=12 3x5=15 3x6=18 3x7=21 3x8=24 3x9=27 4x4=16 4x5=20 4x6=24 4x7=28 4x8=32 4x9=36 5x5=25 5x6=30 5x7=35 5x8=40 5x9=45 6x6=36 6x7=42 6x8=48 6x9=54 7x7=49 7x8=56 7x9=63 8x8=64 8x9=72 9x9=81 for(int i = 1;i<=9;i++){for(int k = 1;k<i;k++){System.out.print("\t");}for(int j = i;j<=9;j++){//一定有九列,而且是從i列到9列System.out.print(i + "x" + j + "=" + (i*j) + "\t");}System.out.println();}
二、while和do-while
while
1.語法結構:
? while(表達式){
? …代碼塊/循環體…
? }
2.理解:
? 表達式的結果必須是boolean類型
? true - 執行代碼塊
? false - 跳出循環
3.死循環:
? while(true){
? System.out.println(“死循環”);
? }
4.需求:
使用while循環遍歷5遍"干就完了"
? int i = 1;
? while(i<=5){
? System.out.println(“干就完了”);
? i++;
? }
5.案例:
我有個夢想,每月存3000,每年遞增1000元,多少個月后存滿20萬
int allMoney = 0;int money = 3000;int month = 0;while(allMoney < 200000){allMoney += money;month++;if(month % 12 == 0){money += 1000;}}System.out.println(month + "個月后存滿20萬");System.out.println(money);
do-while
1.語法結構:
? do{
? …代碼塊/循環體…
? }while(表達式);
2.理解:
? 首先執行一遍代碼塊,再判斷表達式
? 表達式的結果必須是boolean類型
? true - 執行代碼塊
? false - 跳出循環
3.死循環:
? do{
? System.out.println(“死循環”);
? }while(true);
?
? 做實驗:
? do{
? System.out.println(“用良心做教育”);
? }while(false);
4.案例:
奇男子參加學校組織的歌詠比賽,大賽在即,
老師建議:先彩排一次,如果很令人滿意,
以后就不用彩排了,否則每天都排,直到現場表現滿意為止!Scanner scan = new Scanner(System.in);String str;do{System.out.println("奇男子:\"旋轉、跳躍,我閉著眼~~~\"");System.out.println("奇男子:\"老師,您滿意了嗎?\"");str = scan.next();}while(str.equals("不滿意"));
三、for vs while vs do-while
表達式的區別:
? for(初始化變量;判斷條件;更新變量){}
? while(判斷條件){}
? do{}while(判斷條件);
? 共同點:判斷條件的結果必須是boolean類型,true就執行代碼塊,false就跳出循環
執行順序的區別:
? for:先判斷,再執行
? while:先判斷,再執行
? do-while:先執行一遍,再判斷
應用場景的區別:
? 循環次數確定 – for
? 循環次數不確定,并且先判斷再執行 - while
? 循環次數不確定,先執行一遍,再判斷 - do-while
四、特殊的流程控制語句
分類:
? break
? continue
? return
? label
break
1.理解:
作用在循環中,表示跳出整個循環語句
做實驗:
while(true){
System.out.println(“111”);
System.out.println(“222”);
if(true){
break;
}
System.out.println(“333”);
}
2.案例:
循環錄入奇男子同學5門課的成績并計算平均分, 如果某分數錄入為負,停止錄入并提示。
Scanner scan = new Scanner(System.in);boolean flag = true;//true-正常錄入 false-非正常錄入double sum = 0;for(int i = 1;i<=5;i++){System.out.println("請輸入第" + i + "門成績:");double score = scan.nextDouble();if(score < 0){flag = false;break;}sum += score;}if(flag){double avg = sum/5;System.out.println("平均分為:" + avg);}else{System.out.println("分數為負數,停止錄入");}
總結:
1.for循環嵌套 – 重要!!!
2.while和do-while
3.for vs while vs do-while
4.特殊的流程控制語句 – break