循环嵌套 使用情况:有一种重复的情况(循环),而这种重复情况中的每一次重复又对应着另一种重复情况的重复多次。
以下是一些循环嵌套的练习
1 class ForForTest 2 {//以下需求不借助第三方变量实现 3 public static void main(String[] args) 4 { 5 /* 6 需求:画图 7 ***** 8 ***** 9 ***** 10 ***** 11 ***** 12 */ 13 14 for (int x=1;x<=5 ;x++ )//外层循环控制行 15 { 16 for (int y=1;y<=5 ;y++ )//内层循环控制列 17 { 18 System.out.print("*"); 19 } 20 21 System.out.println(); 22 } 23 24 System.out.println("-------------------华丽丽的分割线-------------------"); 25 26 27 /* 28 需求:画图 29 ***** 30 **** 31 *** 32 ** 33 * 34 35 */ 36 37 for (int x = 1;x<=5 ;x++ ) 38 { 39 for (int y=x;y<=5;y++ )//1-5(5个) 2-5(4个) 3-5(3个)... 也可以是1-5(5个)1-4(4个)1-3(3个)... 40 { 41 System.out.print("*"); 42 } 43 44 System.out.println(); 45 } 46 47 48 System.out.println("-------------------华丽丽的分割线-------------------"); 49 50 //也可以是 51 for (int x=0;x<5;x++ ) 52 { 53 for (int y=0;y<5-x ;y++ )//0-4(5个)0-3(4个)0-2(3个)... 54 { 55 System.out.print("*"); 56 } 57 58 System.out.println(); 59 } 60 61 System.out.println("-------------------华丽丽的分割线-------------------"); 62 63 /* 64 需求:画图 65 * 66 ** 67 *** 68 **** 69 ***** 70 71 */ 72 for (int x=1;x<=5 ;x++ ) 73 { 74 for (int y=1;y<=x ;y++ )//1-1 1-2 1-3 1-4 1-5 75 { 76 System.out.print("*"); 77 } 78 79 System.out.println(); 80 } 81 82 System.out.println("-------------------华丽丽的分割线-------------------"); 83 84 for (int x=5;x>=1 ;x-- ) 85 { 86 for (int y=x;y<=5 ;y++ )//5-5 4-5 3-5 2-5 1-5 87 { 88 System.out.print("*"); 89 } 90 91 System.out.println(); 92 } 93 94 System.out.println("-------------------华丽丽的分割线-------------------"); 95 96 97 /* 98 需求:画图 99 54321 100 5432 101 543 102 54 103 5 104 105 */ 106 107 for (int x=1;x<=5 ;x++ ) 108 { 109 for (int y=5;y>=x ;y-- )//5-1 5-2 5-3 5-4 5-5 110 { 111 System.out.print(y); 112 } 113 114 System.out.println(); 115 } 116 117 System.out.println("-------------------华丽丽的分割线-------------------"); 118 119 /* 120 需求:画图 121 54321 122 4321 123 321 124 21 125 1 126 127 */ 128 129 for (int x=5;x>=1 ;x--) 130 { 131 for (int y=x ; y>=1 ; y--) 132 { 133 System.out.print(y); 134 } 135 System.out.println(); 136 } 137 138 System.out.println("-------------------华丽丽的分割线-------------------"); 139 140 /* 141 需求:画图 142 1 143 22 144 333 145 4444 146 55555 147 148 */ 149 150 for (int x=1;x<=5 ;x++ ) 151 { 152 for (int y=1;y<=x ;y++ )//1-1 1-2 1-3 1-4 1-5 153 { 154 System.out.print(x); 155 } 156 157 System.out.println(); 158 } 159 160 System.out.println("-------------------华丽丽的分割线-------------------"); 161 162 for (int x=5; x>=1 ; x-- ) 163 { 164 for (int y=x; y<=5 ; y++ )//5-5 4-5 3-5 2-5 1-5 165 { 166 System.out.print(6-x); 167 } 168 169 System.out.println(); 170 } 171 172 System.out.println("-------------------华丽丽的分割线-------------------"); 173 174 /* 175 需求:画图 九九乘法表 176 177 1*1=1 178 1*2=2 2*2=4 179 1*3=3 2*3=6 3*3=9 180 ...... 181 182 */ 183 184 for (int x=1;x<=9 ;x++ ) 185 { 186 for (int y=1;y<=x ;y++ )//1-1 1-2 1-3 1-4 .... 187 { 188 System.out.print(y+"*"+x+"="+(y*x)+"\t"); 189 190 } 191 192 System.out.println(); 193 } 194 195 System.out.println("-------------------华丽丽的分割线-------------------"); 196 197 //尝试用其他方法,表可以列出来,但是显示是反的。。。。 198 for (int x=9;x>=1 ;x-- ) 199 { 200 for (int y=x;y<=9 ;y++ )//9-9 8-9 7-9 6-9.... 201 { 202 System.out.print((10-y)+"*"+(10-x)+"="+((10-y)*(10-x))+"\t"); 203 //System.out.print(y+"*"+x+"="+(y*x)+"\t"); 204 //System.out.print(x+"*"+y+"="+(y*x)+"\t"); 205 } 206 207 System.out.println(); 208 } 209 210 System.out.println("-------------------华丽丽的分割线-------------------"); 211 212 /* 213 需求:画图 214 * * * * * 215 * * * * 216 * * * 217 * * 218 * 219 220 思路分析:此图可以理解为两个图形构成的 221 * * * * * 222 -* * * * 223 --* * * 224 ---* * 225 ----* 226 1.先把直角三角形画出来 227 2.再把由*和空格组成的倒三角画出 228 */ 229 230 for (int x=0;x<5 ;x++ ) 231 { 232 for (int y=0; y<x;y++ )//0-0 0-1 0-2 0-3 0-4 233 { 234 System.out.print(" "); 235 } 236 237 238 for (int i=x;i<5;i++ )//0-5 1-5 2-5 3-5 4-5 239 { 240 System.out.print("* "); 241 } 242 243 System.out.println(); 244 } 245 246 System.out.println("-------------------华丽丽的分割线-------------------"); 247 248 for (int x=5;x>0 ;x-- ) 249 { 250 for (int y=x;y<5 ;y++ )//5-5 4-5 3-5 2-5 1-5 251 { 252 System.out.print(" "); 253 } 254 255 256 for (int i=0;i<x;i++ )//0-5 0-4 0-3 0-2 0-1 257 { 258 System.out.print("* "); 259 } 260 261 System.out.println(); 262 } 263 264 } 265 266 }
关于转义字符:
常用的有 \n:回车 \t:制表符 \b:退格 \r:按下回车键
注意;每种系统中回车符是不一样的。 windows中回车符其实是两个符号组成的\r\n 。
dos为底层系统,其中回车符只要\r或者\n其中之一就可以识别。
linux中回车符是\n 。
其他流程控制语句:
break注意以下情况 break单独存在时后边不要有语句。
for(int i=0;i<3;i++) { break;//这样是错误的,运行到此直接跳出循环,之后的语句永远都执行不了。 System.out.println("i="+i); } for(int i=0;i<3;i++) { if(i==1) break;//这样是可以的,被if语句控制,但是跳出的作用范围是for语句,而并不是if,break此处只对循环有效。 System.out.println("i="+i); }
break默认情况下跳出的是所在的当前循环,如果出现了嵌套循环,break想要跳出指定的循环,可以通过给循环标号来完成。
//跳出指定循环(循环的标号) loop1: for(int x=0;x<3;x++) {
loop2:for(int y=0;y<5;y++) { System.out.print("y="+y); break loop1;//这时跳出的是为外层的loop1循环 } }
continue注意以下情况,continue的真正作用是结束本次循环,继续下次循环。单独存在时后边不要有语句。
for(int i=0;i<3;i++) { continue;//这样是错误的,运行到此返回到循环后表达式i++,以此往复知道循环条件不成立循环结束,而continue之后的语句永远都执行不了。 System.out.println("i="+i);//这么写,这条语句永远执行不到,会报错。 }
continue的用法
for(int i=0;i<11;i++) { if(i%2==0) continue; System.out.println("i="+i);//打印出0-10中的全部奇数。 }
continue默认操作的是当前的循环,但在嵌套循环中也可以通过循环的标号来操作指定的外层循环。
//结束指定的当前循环,继续到下一次循环(通过循环的标号来指定) loop1: for(int x=0;x<3;x++) { loop2:for(int y=0;y<5;y++) { System.out.print("x="+x); continue loop1;//直接进行到外层loop1的下一次循环。 } }