循环

循环结构

1.根据学生作业完成情况,输入教学评语,决定学生是否需要抄写代码。

package com.xing.demo;

import java.util.Scanner;

public class 检查学生成绩 {
    public static void main(String[] args) {
        //根据学生作业完成情况,输入教学评语,决定学生是否需要抄写代码。
        Scanner input = new Scanner(System.in);
        String answer;
        do{
            System.out.println("开始抄写作业代码");
            System.out.println("请输入评语?y/n");
            answer = input.next();

        }while (!answer.equals("y"));
        System.out.println("恭喜通过了...");
    }
}

开始抄写作业代码
请输入评语?y/n
sjj
开始抄写作业代码
请输入评语?y/n
y
恭喜通过了...

Process finished with exit code 0

2.do while 循环 女朋友心态案例

package com.xing.demo;

import java.util.Scanner;

public class 女朋友dowehile循环 {
    public static void main(String[] args) {
        //女朋友dowehile循环
        Scanner input = new Scanner(System.in);
        String answer;
        do{
            System.out.println("做我女朋友好不好?");
            System.out.println("什么事情不会让你百分之百满意,那几分不满意,让你知道不过如此,没什么可骄傲了");
            System.out.println("回答?:y/n");
            answer = input.next();
        }while (!answer.equals("y"));
        System.out.println("不如意,让你知道,天外有天,人外有人,要一辈子,保持谦卑,喜怒哀乐,平和以对,不可过度");
        System.out.println("恭喜你脱单了!");
    }
}

做我女朋友好不好?
什么事情不会让你百分之百满意,那几分不满意,让你知道不过如此,没什么可骄傲了
回答?:y/n
n
做我女朋友好不好?
什么事情不会让你百分之百满意,那几分不满意,让你知道不过如此,没什么可骄傲了
回答?:y/n
y
不如意,让你知道,天外有天,人外有人,要一辈子,保持谦卑,喜怒哀乐,平和以对,不可过度
恭喜你脱单了!

Process finished with exit code 0

for 循环

1.语法

for(初始条件;循环条件;迭代部分){

//循环操作

}

package com.xing.demo;

public class for循环的使用 {
    //for循环的使用
    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            System.out.println("hello world");
            System.out.println("不要再浪费时间在无意义的地方上");
        }
    }
}
不要再浪费时间在无意义的地方上
hello world
不要再浪费时间在无意义的地方上
hello world
不要再浪费时间在无意义的地方上

Process finished with exit code 0

2.for循环五名同学平均分

package com.xing.demo;

import java.util.Scanner;

public class for循环五名同学平均分 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        
        int sum=0;
        for (int i = 0; i < 5; i++) {
            System.out.println("请输入第"+(i+1)+"个同学成绩");
            int score = input.nextInt();
            sum+=score;
        }
        double avg =(double) sum / 5;
        System.out.println("平均分是:"+avg);
    }
}
请输入第1个同学成绩
1
请输入第2个同学成绩
2
请输入第3个同学成绩
3
请输入第4个同学成绩
4
请输入第5个同学成绩
5
平均分是:3.0

Process finished with exit code 0

3.break语句的使用

作用:跳出,终止switch和循环

package com.xing.demo;

public class break语句 {
    /*## 3.break语句的使用

作用:跳出,终止switch和循环

*/
    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            System.out.println("i="+i);
            if(i==5){
                break; //跳出循环
            }
        }
        System.out.println("程序结束了");
    }
}
i=0
i=1
i=2
i=3
i=4
i=5
程序结束了

4.长跑中途退出例子

package com.xing.demo;

import java.util.Scanner;

public class 长跑中途退出例子 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String answer;
        int count=0;

        do {
            count++;
            System.out.println("小张开始跑:"+count+"圈");
            System.out.println("=================");
            System.out.println("小张跑完了:"+count+"圈");
            System.out.println("还能坚持吗?y/n");
            answer = input.next();
            if(!answer.equals("y")){
                break;//跳出循环
            }

        }while (count<10);
        System.out.println("比赛结束了,小张共跑了:"+count+"圈");


    }
}

小张开始跑:1=================
小张跑完了:1圈
还能坚持吗?y/n
y
小张开始跑:2=================
小张跑完了:2圈
还能坚持吗?y/n
y
小张开始跑:3=================
小张跑完了:3圈
还能坚持吗?y/n
y
小张开始跑:4=================
小张跑完了:4圈
还能坚持吗?y/n
y
小张开始跑:5=================
小张跑完了:5圈
还能坚持吗?y/n
y
小张开始跑:6=================
小张跑完了:6圈
还能坚持吗?y/n
y
小张开始跑:7=================
小张跑完了:7圈
还能坚持吗?y/n
y
小张开始跑:8=================
小张跑完了:8圈
还能坚持吗?y/n
n
比赛结束了,小张共跑了:8圈

Process finished with exit code 
 

5.continue:结束本次循环,继续执行下一次循环

continue:结束本次循环,继续执行下一次循环

package com.xing.demo;

public class continue结束本次循环继续执行下一次循环 {
    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            if(i==5){
                continue;
            }
            System.out.println("i:"+i);
        }
        System.out.println("程序结束了");
    }
}
i:0
i:1
i:2
i:3
i:4
i:6
i:7
i:8
i:9
程序结束了
package com.xing.demo;

import java.util.Scanner;

public class 口渴喝水继续跑 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String answer;
        int  count=0;
        do {
            count++;
            System.out.println("开始跑:"+count+"圈");
            System.out.println("=================");
            System.out.println("跑完了:"+count+"圈");
            System.out.println("口渴吗?y/n");
            answer = input.next();
            if(!answer.equals("y")){
                continue;//跳出本次循环
            }
            System.out.println("喝水----------->");
        }while (count<10);
    }
}

跑完了:2圈
口渴吗?y/n
y
喝水----------->
开始跑:3=================
跑完了:3圈
口渴吗?y/n

6.喝酒令

遇到7,7的倍数跳过去,一行显示6个

package com.xing.demo;

public class 喝酒令 {
    //遇到7,7的倍数跳过去,一行显示6个
    public static void main(String[] args) {
        int count=0; //计数器
        for (int i = 1; i < 100; i++) {
            if(i%7==0||1%10==7||i/10==7){
                continue;
            }
            System.out.print(i+"\t");
            count++;
            if(count%6==0){
                System.out.println();
            }
            

        }
    }
}

1	2	3	4	5	6	
8	9	10	11	12	13	
15	16	17	18	19	20	
22	23	24	25	26	27	
29	30	31	32	33	34	
36	37	38	39	40	41	
43	44	45	46	47	48	
50	51	52	53	54	55	
57	58	59	60	61	62	
64	65	66	67	68	69	
80	81	82	83	85	86	
87	88	89	90	92	93	
94	95	96	97	99	
Process finished with exit code 0
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值