嵌套循環的使用
1.嵌套循環:將一個循環結構A聲明在另一個循環結構b的循環體中,就構成了循環嵌套
2.外層循環:循環體b;
內層循環:循環體a;
打印一個正方形
3.外層控制行數;內行控制列
for (int y = 1; y<=9;y++){ for (int L = 1; L <=9;L++){ System.out.print("*"+" "); } System.out.println(); } 測試 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
打印直角三角形
for (int t=1;t<10;t++) { for (int j = 1; j <10; j++) { if (j<t||j==t) { System.out.print("* " ); } } System.out.println(); } 測試 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
打印乘法口訣表
for (int t = 1; t < 10; t++) { for (int j = 1; j < 10; j++) { if (j < t || j == t) { int g = t * j; System.out.print(t + "*" + j + "=" + g + " "); } } System.out.println(); } 測試 1*1=1 2*1=2 2*2=4 3*1=3 3*2=6 3*3=9 4*1=4 4*2=8 4*3=12 4*4=16 5*1=5 5*2=10 5*3=15 5*4=20 5*5=25 6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36 7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49 8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64 9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
打印三角形 ,打印乘法口訣表,第二種方法;
//打印直角三角形 for (int t = 1; t < 10; t++) { for (int j = 1; j <= t; j++) { //if (j < t || j == t) { System.out.print("* "); //} } System.out.println(); } System.out.println("\n"); //乘法口訣表; for (int t = 1; t <10; t++) { for (int j = 1; j <= t; j++) { // if (j < t || j == t) { int g = t * j; System.out.print(t + "*" + j + "=" + g + " "); // } } System.out.println(); } 測試 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 1*1=1 2*1=2 2*2=4 3*1=3 3*2=6 3*3=9 4*1=4 4*2=8 4*3=12 4*4=16 5*1=5 5*2=10 5*3=15 5*4=20 5*5=25 6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36 7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49 8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64 9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
打印倒立三角形
System.out.println("\n"); for (int t=1;t<10;t++){ for (int j = 1;j<= 10-t;j++){ System.out.print("* "); } System.out.println(); } 測試 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
打印100以內的質數
for (int t = 2 ; t<100; t++ ){ int sun = 0; for (int y =2 ; y<t;y++){ if (t%y == 0) { sun ++; } } if (sun==0){ System.out.println(t); } } 測試 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
優化,也要根據實際情況數值如果太小不建議使用開方,它反而會慢
long start = System.currentTimeMillis(); for (int t = 2 ; t< 100000; t++ ){ int sun = 0; for (int y =2 ; y< t;y++){ //for (int y =2 ; y<=Math.sqrt(t);y++){// 使用開方 if (t%y == 0) { sun ++; //break;//優化 } } if (sun==0){ System.out.println(t); } } long stop = System.currentTimeMillis(); long s = stop - start; System.out.println("所花費的時間:"+s); //不優化時間21609;1打開break,1583;1使用開方121;2
關鍵字break與continue的使用;關鍵字的后面不能使用語句
//關鍵字break與continue的使 for (int t= 1; t<10 ;t++){ if (t%4==0){ //break;//終止當前循環體,結果123 continue;//打斷本次循環,繼續下一次循環,結果1235679 } System.out.print(t); } 測試 1235679
示例2
k:for(int i = 1;i<=4;i++){ for (int j=1;j<=10;j++){ if (j%4==0){ //break k;//終止外層循環體;結果123 continue k;//打斷外層循環體的本次循環;結果123123123123 } System.out.print(j); } } 測試 123123123123 k:for(int i = 1;i<=4;i++){ for (int j=1;j<=10;j++){ if (j%4==0){ break k;//終止外層循環體;結果123 //continue k;//打斷外層循環體的本次循環;結果123123123123 } System.out.print(j); } } 測試 123