一、循环
循环:反复的去执行一段相同或相似的代码
- 循环三要素:
-
1.循环变量的初始化
-
2.循环的条件(以循环变量为基础)
-
3.循环变量的改变(向着循环的结束变)
循环变量:在整个循环过程中反复改变的那个数
例如:
循环变量为:所跑圈数count
第一要素为:int count=0 //初始零圈
第二要素为:count<3? //不够三圈要继续跑
第三要素为:count++ //每跑一圈圈数加一
二、循环结构
结构分为三种:
循环结构分为三种:while(先判断再执行,有可能一次不执行);do while(先执行再判断,至少执行一次) ;for;
while结构
if与while的结构对比
if(boolean){//只判断一次
语句块 //最多执行一次
}
while(boolean){//判断多次,直至boolean出现false结果
语句块 //为true则走,执行多次
}
while执行过程:判断boolean值,若为true则执行语句块,再判断boolean的值若为true则执行语句块,如此反复,直至boolean值为false时循环结束。
猜数游戏:
package day005;
import java.util.Scanner;
public class GuessDemo {
//猜数游戏
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
int num=(int)(Math.random()*1000+1);
System.out.println("猜整数");
int guess=scan.nextInt();
while(guess!=num){
System.out.println("猜错了");
if(guess>num){
System.out.println("大了哦");
}else{
System.out.println("小了哦");
}
guess=scan.nextInt();
}
System.out.println("猜对了");
}
}
补充:系统随机生成数—法一
-
Math.random() //0.0---0.999999999999999...
-
*1000----------------0.0---999.999999999999.....
-
+1-------------------1.0---1000.99999999999.....
-
(int)-----------------1---1000
do while
- 1.语法
-
do{
-
语句块
-
}while(boolean);
- 2.执行过程
-
先执行语句块,再判断boolean的值,若为true则
-
再次执行语句块,再次判断boolean,若为true则
-
再次执行语句块,再次判断boolean,如此反复,
-
直至boolean为false循环结束。
Scanner scan=new Scanner(System.in);
int num=(int)(Math.random()*1000+1);
System.out.println("猜整数");
int guess;
do{
System.out.println("guess");
guess=scan.nextInt();
}while(guess!=num);
要素1和要素3相同时,首选do…while
for
与次数相关更适合
*for结构
- 1.语法:
-
// 1 2 3
- for(要素①;要素②;要素③){
-
语句块/循环体--------反复执行的代码 4
-
}
- 2.执行过程:1243243、243、243…2
for(int time=0;time<10;time++){
System.out.println("行动是成功的阶梯");
}//10time
//1 for结构的几种特殊写法----了解
int time=0//一要素可以调上面去
for(;time<10;time++) {
System.out.println("行动是成长的阶梯");
}
for(int time=0;time<10;) {
System.out.println("行动是成长的阶梯");
time++;//三要素可以放里面来
}
for(;;){//死循环,此代码后面全部报错
System.out.println("我要学习");
}
for(int i=0,j=5;i<5;i+=2,j-=2){
//第一、三要素可以逗号,第二要素不允许使用逗号
}
//2、累加和:1+2+3+....+100
int num=0; //存和
for(int i=1;i<=100;i++){
num=num+i;
}
System.out.println(num);
break 跳出循环
练习
int time=10;
while(time>=1){
System.out.println("行动是成功的阶梯"+time);
time--;
}
System.out.println("over");
/*执行过程:
* time=10
* t,输出,9
* t,输出,8
* t,输出,7
* t,输出,6
* t,输出,5
* t,输出,4
* t,输出。3
* t。输出,2
* t,输出,1
* t,输出,0
* f,结束;
*/
/*5 int a=0;//10time
* while(a<10){
* System.out.println("若为true则执行语句块");
* a++;
* }
* System.out.println("over");
*/
/*4 int b=1;//9time
* while(b<10){
* System.out.println("若为false则bu执行语句块");
* b++;
* }
* System.out.println("over");
*/
/*3 int time=0;//10time
* while(time<10){
* System.out.println("行动是成功的阶梯");
* time++;
* }
* System.out.println("over");
*/
/*2 int time=10;//10time
* while(time>=1){
* System.out.println("行动是成功的阶梯"+time);
* time--;
* }
* System.out.println("end");
*/
/*1 int time=500;//600time
while(time>=-99){
int a=time+100;
System.out.println("每一次敲击键盘都是通往成功的更近一步"+a);
time--;
}
System.out.println("over");*/
int num=1;
while(num<=9){
System.out.println(num+"*9="+num*9);
num++;
}
System.out.println("end");
int number=9;
while(number>=1){
System.out.println(number+"*9="+number*9);
number--;
}
System.out.println("over");
/*执行过程:
* number=9
* t,输出,8
* t,输出,7
* t,输出,6
* t,输出,5
* t,输出,4
* t,输出,3
* t,输出。2
* t。输出,1
* t,输出,0
* f,结束;
* /
/*5 int num=9;
* while(num>0){
* System.out.println(num+"*9="+num*9);
* num--;
* }
* System.out.println("end");
*/
/*4 int num=1;
* while(num<10){
* System.out.println(num+"*9="+num*9);
* num++;
* }
* System.out.println("end");
*/
/*3 int num=1;
* while(num<=9){
* System.out.println(num+"*9="+num*9);
* num++;
* }
* System.out.println("over");
*/
/*2 int num=9;
* while(num>0){
* System.out.println(num+"*9="+num*9);
* num--;
* }
* System.out.println("over");
*/
/*1 int num=9;
* while(num>=1){
* System.out.println(num+"*9="+num*9);
* num--;
* }
* System.out.println("end");
*
*/
/*5 System.out.println("猜数");
* int guess=scan.nextInt();
* while(guess!=200){
* System,out.println("错了");
* guess=scan.nextInt();
* }
* System.out.println("对了");
*/
/*4 System.out.println("猜数");
* int guess=scan.nextIn();
* while(guess!=250){
* System.out.println("错了");
* guess=scan.nextInt();
* }
* System.out.println("对了");
*/
/*3 System.out.println("猜数");
* int guess=scan.nextIn();
* while(guess!=260){
* System.out.println("错了");
* guess=scan.nextInt();
* }
* System.out.println("对了");
*/
/*2 System.out.printlm("猜数");
* int guess=scan.nextIn();
* while(guess!=400){
* Sytem.out.println("错了");
* guess=scan.nextln();
* }
* System.out.println("对了");
*/
/*1 System.out.println("猜数");
* int guess=scan.nextIn();
* while(guess!=500){
* Sytem.out.println("错了");
* guess=scan.nextln();
* }
* System.out.println("对了");
*/
Scanner scan=new Scanner(System.in);
int num=(int)(Math.random()*1000+1);
System.out.println("猜整数");
int guess=scan.nextInt();
while(guess!=num){
System.out.println("猜错了");
if(guess>num){
System.out.println("大了哦");
}else{
System.out.println("小了哦");
}
System.out.println("again");
guess=scan.nextInt();
}
System.out.println("猜对了");
/*5 int num=(int)(Math.random()*1000+1);
* System.out.println("guess");
* int guess=scan.nextInt();
* while(guess!=num){
* System.out.println("false");
* if(guess>num){
* System.out.println("da");
* }else{
* System.out.println("xiao");
* }
* System.out.println("again");
* guess=scan.nextInt();
* }
* System.out.println("win");
*/
/*4 int num=(int)(Math.random()*1000+1);
* System.out.println("guess");
* int guess=scan.nextInt();
* while(guess!=num){
* System.out.println("false");
* if(guess>num){
* System.out.println("da");
* }else{
* System.out.println("xiao");
* }
* System.out.println("again");
* guess=scan.nextInt();
* }
* System.out.println("win");
*
*/
/*3 int a=(int)(Math.random()*1000+1);
* System.out.println("guess");
* int b=scan.nextInt();
* while(a!=b){
* System.out.println("false");
* if(b>a){
* System.out.println("da");
* }else{
* System.out.println("xiao");
* }
* System.out.println("again");
* b=scan.nextInt();
* }
* System.out.println("win");
*/
/*2 int a1=(int)(Math.random()*1000+1);
* System.out.println("guess");
* int a2=scan.nextInt();
* while(a1!=a2){
* System.out.println("false");
* if(a2>a1){
* System.out.println("da");
* }else{
* System.out.println("xiao");
* }
* System.out.println("again");
* a2=scan.nextInt();
* }
* System.out.println("win");
*/
/*1 int num=(int)(Math.random()*1000+1);
* System.out.println("guess");
* int guess=scan.nextInt();
* while(num!=guess){
* System.out.println("false");
* if(guess>num){
* System.out.println("da");
* }else{
* System.out.println("xiao");
* }
* System.out.println("again");
* guess=scan.nextInt();
* }
* System.out.println("win");
*/
``/*5 int a;
* do{
* System.out.println("测数");
* a=scan.nextInt();
* if(a>200){
* System.out.println("max");
* }else if(a<200){
* System.out.println("min");
* }else{
* System.out.println("win");
* }
* }while(a!=200);
*/
/*4 int b;
* do{
* System.out.println("测数");
* b=scan.nextInt();
* if(b>250){
* System.out.println("da");
* }else if(b<250){
* System.out.println("xiao");
* }else{System.out.println("win");
* }
* }while(b!=250);
*/
/*3 int num;
* do{System.out.peintln("guess");
* num=scan.nextInt();
* if(num>300){
* System.out.println("da");
* }else if(num<300){
* System.out.println("xiao");
* }else{System.out.println("win");
* }while(num!=300);
*/
/*2 int num;
* do{
* System.out.println("guess");
* num=scan.nextInt();
* if(num>360){
* System.out.println("da");
* }else if(num<360){
* System.out.println("xiao");
* }else{System.out.println("win");
* }while(num!=360);
*
*/
/*1 int num;
* do{
* System.out.println("guess");
* num=scan.nextInt();
* if(num>42){
* System.out.println("da");
* }else if(num<42){
* System.out.println("xiao");
* }else{System.out.println("win");
* }while(num!=42);
*
*/
```java
/*5 int guess;
* do{
* System.out.println("guess");
* guess=scan.nextInt();
* }while(guess!=200);
*/
/*4 int a;
* do{
* System.out.println("猜数");
* a=scan.nextInt();
* }while(a!=250);
*/
/*3 int b;
* do{
* System.out.println("猜数");
* b=scan.nextInt();
* }while(b!=300);
*/
/*2 int number;
* do{
* System.ouy.println?("猜数");
* number=scan.nextInt();
* }while(number!=250);
*/
/*1 int guess;
* do{
* System.out.println("guess");
* guess=scan.nextInt();
* }while(guess!=260);
*/
int guess;
do{
System.out.println("猜整数");
guess=scan.nextInt();//1+3
if(guess==-1){
break;
}
if(guess>num){
System.out.println("大了");
}else if(guess<num){
System.out.println("小了");
}else{
System.out.println("win");
}
}while(guess!=num);//2
/*5 int a;
* do{
* System.out.println("guess");
* a=scan.nextInt();
* if(a==-1){
* break;
* }
* if(a>100){
* System.out.println("大了");
* }else if(a<100){
* System.out.println("小了");
* }else{System.out.println("对了");
* }
* }while(a!=100);
*/
/*4 int a;
* do{System.out.println("guess");
* a=scan.nextInt();
* if(a==-1){
* break;
* }
* if(a>200){
* System.out.println("大了");
* }else if(a<200){
* System.out.println("小了");
* }else{
* System.out.println("win");
* }
* }while(a!=200);
*/
/*3 int =k;
* do{System.out.println("caishuzi");
* k=scan.nextInt();
* if(k==-1){
* break;
* }
* if(k>300){
* System.out.println("大了");
* }else if(k<300){
* System.out.println("小了");
* }else{
* System.out.println("正确");
* }
* }while(k!=300);
*/
/*2 int mn;
* do{System.out.println("caishuz");
* mn=scan.nextInt();
* if(mn==-1){
* break;
* }
* if(mn>400){
* System.out.println("大了");
* }else if(mn<400){
* System.out.println("小了");
* }else{
* System.out.println("正好");
* }
* }while(mn!=400);
*/
/*1 int zx;
* do{System.out.println("csz");
* zx=scan.nextInt();
* if(zx==-1){
* break;
* }
* if(zx>500){
* System.out.println("大了");
* }else if(mn<400){
* System.out.println("小了");
* }else{
* System.out.println("正好");
* }
* }while(zx!=500);
*/
//for中的num----作用范围仅在for中
for(int num=1;num<=9;num++){
System.out.println(num+"*9="+num*9);
}
//System.out.println(num+"*9="+num*9);超出范围
for(int num=1;num<=9;num+=2){
System.out.println(num+"*9="+num*9);
}
for(int num=9;num>=1;num--){
System.out.println(num+"*9="+num*9);
}
for(int time=0;time<10;time++){
System.out.println("行动是成功的阶梯");
}
/*time=0 true 输出
* time=1 true 输出
* time=2 true 输出
* time=3 true 输出
* time=4 true 输出
* time=5 true 输出
* time=6 true 输出
* time=7 true 输出
* time=8 true 输出
* time=9 true 输出
* time=10 false end
*/
/*5 for(int a=1;a<=9;a++){
* System.out.println(num+"*9="+num*9);
* }
* for(int a=1;a<=9;a+=2){
* System.out.println(num+"*9="+num*9);
* }
* for(int a=9;a>=1;a--){
* System.out.println(num+"*9="+num*9);
* }
* for(int time=0;time<=9;time++){
* System.out.println(num+"行动是成功的阶梯");
* }
*/
/*4 for(int a=1;a<10;a++){
* System.out.println(num+"*9="+num*9);
* }
* for(int a=1;a<10;a+=2){
* System.out.println(num+"*9="+num*9);
* }
* for(int a=9;a>=1;a--){
* System.out.println(num+"*9="+num*9);
* }
* for(int number=1;number<=100;number++){
* System.out.println(num+"行动是成功的阶梯");
* }
*/
/*3 for(int n=1;n<=9;n++){
* System.out.println(num+"*9="+num*9);
* }
* for(int n=1;n<=9;n+=10){
* System.out.println(num+"*9="+num*9);
* }
* for(int n=9;n>0;n--){
* System.out.println(num+"*9="+num*9);
* }
* for(int num=100;n>0;n--){
* System.out.println("行动是成功的阶梯"+num);
* }
*/
/*2 for(int d=1;d<10;d++){
* System.out.println(num+"*9="+num*9);
* }
* for(int d=1;d<10;d+=2){
* System.out.println(num+"*9="+num*9);
* }
* for(int d=9;d>0;d--){
* System.out.println(num+"*9="+num*9);
* }
* for(int time=50;d>0;d-=2){
* System.out.println("行动是成功的阶梯");
* }
*/
/*1 for(int k=1;k<10;k++){
* System.out.println(num+"*9="+num*9);
* }
* for(int k=1;k<=9;k+=2){
* System.out.println(num+"*9="+num*9);
* }
* for(int k=9;k>=1;k--){
* System.out.println(num+"*9="+num*9);
* }
* for(int number=1;number>=100;number+=5){
* System.out.println("行动是成功的阶梯"+num);
* }
*/
int num=0; //存和
for(int i=1;i<=100;i++){
num=num+i;
}
System.out.println(num);
/*执行过程
* i=1 num=0+1
* i=2 num=0+1+2
* i=3 num=0+1+2+3
* ...
* i=100 num=0+1+...+99+100
* i=101 num!<=100 输出num
*/
/*5 int t=0;
* for(int a=1;a<=100;a++){
* t=t+a;
* }
* System.out.println(t);
*/
/*4 int n=0;
* for(int c=1;c<=100,c==){
* n=n+c;
* }
* System.out.println(n);
*/
/*3 int num=0;
* for(int i=1;c<=100;c++){
* num=num=i;
* }
* System.out.println(num);
*/
/*2 int k=0;
* for(int q=1;q<=100;q++){
* k=q+k;
* }
* System.out.println(k);
*/
/*1 int j=0;
* for(int a=1;a<=100;a++){
* j=a+j;
* }
* System.out.println(j);
*/
转载:https://blog.csdn.net/litter_m/article/details/105285281
查看评论