循环
1.什么是循环
三要素:起点,循环体,截止
2.怎么去循环
![](https://img-blog.csdnimg.cn/img_convert/4a44642178c6f59f9b535ed7ee1180ec.png)
3.java中的循环语法
a.while循环
![](https://img-blog.csdnimg.cn/img_convert/0067eeabebd25193dad456dd972d7e39.png)
实现:
![](https://img-blog.csdnimg.cn/img_convert/f07aa2fb7fdfb6e1268da68222f97490.png)
![](https://img-blog.csdnimg.cn/img_convert/a86386acdd1595b8b8c488aeb918b918.png)
![](https://img-blog.csdnimg.cn/img_convert/2b6e37f0e782a34b349322fbc593aedb.png)
![](https://img-blog.csdnimg.cn/img_convert/02fb6d01c58aeca7e70356069eaab4e8.png)
b.do-while循环
![](https://img-blog.csdnimg.cn/img_convert/ebfd382e8b936453dd3183e2c5208089.png)
![](https://img-blog.csdnimg.cn/img_convert/db36ccf6e69eb3eb0098e48a9a8f550d.png)
while循环和do-while循环的区别和联系:
while:当判断条件不满足的时候,一次也不执行;while循环的执行次数是0-n;
![](https://img-blog.csdnimg.cn/img_convert/b6eb4f148f2a92611d0fc89a19550630.png)
do-while:当判断条件不满足时,至少执行一次;do-while的循环范围是1-n
![](https://img-blog.csdnimg.cn/img_convert/20fec565de9e79f699edf9a463e6b078.png)
c.for循环(foreach)
![](https://img-blog.csdnimg.cn/img_convert/3a5bdba99b03c6a33630abbeb7490c94.png)
![](https://img-blog.csdnimg.cn/img_convert/0e642f0906473da0564ed38991762647.png)
执行顺序
![](https://img-blog.csdnimg.cn/img_convert/cba83c295badd007aa726147128004c4.png)
![](https://img-blog.csdnimg.cn/img_convert/3be1b459b23e55d7ed477f06e3ebf240.png)
注意的地方
![](https://img-blog.csdnimg.cn/img_convert/ae5f1ed01f2b12ce7234544598ed27fb.png)
![](https://img-blog.csdnimg.cn/img_convert/22cdd389dfba6c98c538e95d47fc1a06.png)
![](https://img-blog.csdnimg.cn/img_convert/6759cf55ebbd083c0f2116e3f2409147.png)
![](https://img-blog.csdnimg.cn/img_convert/4fe4de976fb5c0c7219c9fb925c16c67.png)
![](https://img-blog.csdnimg.cn/img_convert/a9ebc924bd33f04677cc6a2a40c40329.png)
![](https://img-blog.csdnimg.cn/img_convert/dbbff7cef5d735f876d6241f09e230f8.png)
d.循环嵌套
内层循环循环一轮,外层循环循环一次
简易三层之内
一个固定代码:
![](https://img-blog.csdnimg.cn/img_convert/21050e0a2cdeb304a95443b06e58aef6.png)
时分秒计时:
![](https://img-blog.csdnimg.cn/img_convert/665e23a6fe78ed7166d9cec33e288455.png)
![](https://img-blog.csdnimg.cn/img_convert/d1beb45029c2eb07f5033d0d7fafb99d.png)
e.两个关键字
跳过本次循环:continue,跳过本次循环,后置内容不再执行,前置内容仍然会执行。
![](https://img-blog.csdnimg.cn/img_convert/f1ec746672569a54d299f68c4159e318.png)
跳出(终止)循环:break,终止循环,后续的所有次数的循环都不再执行。在调用break之前的依然正常执行,在调用break之后的,以及后续的所有轮,都结束。
![](https://img-blog.csdnimg.cn/img_convert/c182fb9ba3f486343de707763e9daa4e.png)
画星星
正方形
思路:
先画一行,这一行是依次打印5个星星
一行打印完毕之后要换行
准备外层循环,意味着打印多少行
![](https://img-blog.csdnimg.cn/img_convert/c36a48fc8ffc065c518b7696a91c8773.png)
![](https://img-blog.csdnimg.cn/img_convert/293c180bd34f64db9d7aa869f5598e79.png)
直角三角形
思路:
和正方形一样,同样是分为一行多少个星星,总共若干行
内层的循环上限和外层的循环当前次数有关
![](https://img-blog.csdnimg.cn/img_convert/a3b0bee9409ccc87419c3663bc50a6f5.png)
或者:
![](https://img-blog.csdnimg.cn/img_convert/35adbdcdcd311428b55eaa3c128ab613.png)
![](https://img-blog.csdnimg.cn/img_convert/c4d795183ab8310e420410131e38f5dc.png)
倒直角三角形
思路:
在正直角的基础上,内层是倒序循环/也可以是增序,因为是数量越来越少
其他的和正直角一样
![](https://img-blog.csdnimg.cn/img_convert/97c57be10be02a10ed14a1b1049c65fd.png)
或者:
![](https://img-blog.csdnimg.cn/img_convert/5732311da33445af583b8f03561cbc6b.png)
![](https://img-blog.csdnimg.cn/img_convert/0a054b4666431bc4b970cfdf3185bfae.png)
![](https://img-blog.csdnimg.cn/img_convert/d741b37058ad38dbf01c9f8055ee1803.png)
反直角三角形
思路:
和正直角基本一致
每一行依然是五个符号,只不过是由空格和星星组合构成,是空格加星星的总和
最后一行,可以看出是0个空格+5个星星
![](https://img-blog.csdnimg.cn/img_convert/34b090d482cc187e60087082511365bb.png)
或者:
![](https://img-blog.csdnimg.cn/img_convert/ee7ad1d0a942c496700d33ce89dd6acc.png)
![](https://img-blog.csdnimg.cn/img_convert/fbcb77d909354887e4b6de57f9c01832.png)
杨辉三角
思路:
在正直角三角形的基础上
每一行的公式为:2a+1
![](https://img-blog.csdnimg.cn/img_convert/4a8402989ce73e29ead954f103ee8939.png)
或者:
![](https://img-blog.csdnimg.cn/img_convert/d5e0a9d2991bda69788a1ab64bf423e2.png)
![](https://img-blog.csdnimg.cn/img_convert/556179b867ebc22e1df6dedefeced4a4.png)
一个小练习:
做一个简化版的体育彩票
规则:3+1,前3位,不看顺序,中1个10元,中2个100元,中3个1000元,最后一个看顺序,全中100000元
packagecom.qf.circulate;
importjava.util.Random;
importjava.util.Scanner;
publicclassBodyColor {
publicstaticvoidmain(String[] args) {
//模拟一个体彩
//3+1
//生成随机数:new Random().nextInt()
Scannerscan=newScanner(System.in);
//生成奖池
while(true) {
System.out.println("欢迎使用割韭菜彩票系统!");
//生成第一个
intnum1=newRandom().nextInt(100);
//生成第二个
intnum2 =0;
while(true) {
num2=newRandom().nextInt(100);
//判断生成的第一个值和第二个是否相同,若不相同则跳出此循环
if(num2!=num1) {
break;
}
}
//生成第三个
intnum3=0;
while(true) {
num3=newRandom().nextInt(100);
//判断生成的第三个值和第一、二个是否相同,若不相同则跳出此循环
if(num3!=num1
&&num3!=num2) {
break;
}
}
//生成第四个
intnum4=0;
while(true) {
num4=newRandom().nextInt(100);
//判断生成的第四个值和第一、二、三个是否相同,若不相同则跳出此循环
if(num4!=num1
&&num4!=num2
&&num4!=num3) {
break;
}
}
System.out.println("奖池:"+num1+" "+num2+" "+num3+" "+num4);
//break;
//选号
System.out.print("请选号(一):");
intchoNum1=scan.nextInt();
//选号二
intchoNum2=0;
while(true) {
System.out.print("请选号(二):");
choNum2=scan.nextInt();
if(choNum2!=choNum1) {
break;
}
}
//选号三
intchoNum3=0;
while(true) {
System.out.print("请选号(三):");
choNum3=scan.nextInt();
if(choNum3!=choNum1
&&choNum3!=choNum2) {
break;
}
}
//选号四
intchoNum4=0;
while(true) {
System.out.print("请选号(四):");
choNum4=scan.nextInt();
if(choNum4!=choNum1
&&choNum4!=choNum2
&&choNum4!=choNum3) {
break;
}
}
System.out.println("选号:"+choNum1+" "+choNum2+" "+choNum3+" "+choNum4);
//兑奖
intcount=0;
booleanflag=true;
if(choNum1==num1) {
count++;
if(choNum2==num2) {
count++;
if(choNum3==num3) {
count++;
if(choNum4==num4) {
count++;
}else {
flag=false;
}
}else {
flag=false;
}
}else {
flag=false;
}
}else {
flag=false;
}
//中奖信息
switch(count){
case1:
System.out.println("恭喜中奖10元!");
break;
case2:
System.out.println("恭喜中奖100元!");
break;
case3:
System.out.println("恭喜中奖1000元!");
break;
case4:
System.out.println("恭喜中奖100000元!");
break;
}
}
}
}
![](https://img-blog.csdnimg.cn/img_convert/bc295819790b62cbb93abf6b5b86e477.png)
改造:
packagecom.qf.circulate;
importjava.util.Random;
importjava.util.Scanner;
publicclassBodyColor {
publicstaticvoidmain(String[] args) {
//模拟一个体彩
//3+1
//生成随机数:new Random().nextInt()
//生成奖池
booleanflag1=true;
while(flag1) {
Scannerscan=newScanner(System.in);
System.out.println("请选择:输入 1.开始游戏\t输入2.退出游戏");
intbegin=scan.nextInt();
switch(begin) {
case1:
System.out.println("欢迎使用割韭菜彩票系统!");
//生成第一个
intnum1=newRandom().nextInt(100);
//生成第二个
intnum2 =0;
while(true) {
num2=newRandom().nextInt(100);
//判断生成的第一个值和第二个是否相同,若不相同则跳出此循环
if(num2!=num1) {
break;
}
}
//生成第三个
intnum3=0;
while(true) {
num3=newRandom().nextInt(100);
//判断生成的第三个值和第一、二个是否相同,若不相同则跳出此循环
if(num3!=num1
&&num3!=num2) {
break;
}
}
//生成第四个
intnum4=0;
while(true) {
num4=newRandom().nextInt(100);
//判断生成的第四个值和第一、二、三个是否相同,若不相同则跳出此循环
if(num4!=num1
&&num4!=num2
&&num4!=num3) {
break;
}
}
//System.out.println("奖池:"+num1+" "+num2+" "+num3+" "+num4);
//break;
//选号
System.out.print("请选号(一):");
intchoNum1=scan.nextInt();
//选号二
intchoNum2=0;
while(true) {
System.out.print("请选号(二):");
choNum2=scan.nextInt();
if(choNum2!=choNum1) {
break;
}
}
//选号三
intchoNum3=0;
while(true) {
System.out.print("请选号(三):");
choNum3=scan.nextInt();
if(choNum3!=choNum1
&&choNum3!=choNum2) {
break;
}
}
//选号四
intchoNum4=0;
while(true) {
System.out.print("请选号(四):");
choNum4=scan.nextInt();
if(choNum4!=choNum1
&&choNum4!=choNum2
&&choNum4!=choNum3) {
break;
}
}
System.out.println("选号:"+choNum1+" "+choNum2+" "+choNum3+" "+choNum4);
//兑奖
intcount=0;
booleanflag=true;
if(choNum1==num1) {
count++;
if(choNum2==num2) {
count++;
if(choNum3==num3) {
count++;
if(choNum4==num4) {
count++;
}else {
flag=false;
}
}else {
flag=false;
}
}else {
flag=false;
}
}else {
flag=false;
}
//中奖信息
switch(count){
case1:
System.out.println("恭喜中奖10元!");
break;
case2:
System.out.println("恭喜中奖100元!");
break;
case3:
System.out.println("恭喜中奖1000元!");
break;
case4:
System.out.println("恭喜中奖100000元!");
break;
default:
System.out.println("没中奖,欢迎下次再来!");
break;
}
break;
default:
System.out.println("可恶这个韭菜跑了!!!>_<");
flag1=false;
break;
}
}
}
}
![](https://img-blog.csdnimg.cn/img_convert/73bfad49a0da4955fd163428153e6eac.png)
作业:
请打印99乘法表(必做)
packagecom.qf.circulate;
publicclassMultiplicationTable {
publicstaticvoidmain(String[] args) {
// TODO Auto-generated method stub
inti=1;
while(i<10) {
intj=1;
while(j<=i) {
System.out.print(j+"*"+i+"="+i*j+"\t");
j++;
}
System.out.println();
i++;
}
}
}
![](https://img-blog.csdnimg.cn/img_convert/3ae53e72ee28ad89b4398b8ddf3a1688.png)
打印一棵圣诞树(选做)
packagecom.qf.circulate;
publicclassChristmasTree {
publicstaticvoidmain(String[] args) {
// TODO Auto-generated method stub
inti=0;
while(i<4) {
intn=30;
while(n>i) {
System.out.print(" ");
n--;
}
intj=0;
while(j<=i*2) {
System.out.print("*");
j++;
}
System.out.println();
i++;
}
inta1=1;
while(a1<6) {
intn1=30;
while(n1>a1*2-1) {
System.out.print(" ");
n1--;
}
intb1=1;
while(b1<a1*4) {
System.out.print("*");
b1++;
}
System.out.println();
a1++;
}
inta2=1;
while(a2<7) {
intn2=30;
while(n2>a2*2) {
System.out.print(" ");
n2--;
}
intb2=0;
while(b2<a2*4) {
System.out.print("*");
b2++;
}
System.out.println();
a2++;
}
inta3 =1;
while(a3<8) {
intn3=26;
while(n3>0) {
System.out.print(" ");
n3--;
}
intj3=8;
while(j3>0) {
System.out.print("*");
j3--;
}
System.out.println();
a3++;
}
}
}
![](https://img-blog.csdnimg.cn/img_convert/840e4efc209044846356bff0ad4ac7fe.png)
思考题:打印1000以内的水仙花数(一个单循环)
packagecom.qf.circulate;
publicclassDaffodil {
publicstaticvoidmain(String[] args) {
// TODO Auto-generated method stub
inti=1000;
System.out.print("一千以内的水仙花数是:");
while(i>1) {
intind=i%10;
intten=i/10%10;
inthub=i/100%10;
intsum=ind*ind*ind+ten*ten*ten+hub*hub*hub;
if(i==sum) {
System.out.print(sum+" ");
}
i--;
}
}
}
![](https://img-blog.csdnimg.cn/img_convert/77a672e83a108570e76abbbea303588e.png)