Java 入门 8 循环语句 for 循环 while 循环

 

 

public class Demo2 {
    public static void main(String[] args) {
//        目标 学会使用for 循环 并理解他的执行过程
//        需求  输出3次helloworld
        for (int i = 0; i < 3; i++) {
            System.out.println("helloworld" + i);
        }
//        需求  求1-5之间的数据和,并把求和结果在控制台输出
        int sum = 0;
        for (int i = 1; i <= 5; i++) {
            sum += i;
        }
        System.out.println(sum);

//        求1-10之间的技术和,并把他们求和结果输出在控制台
        int sum1 = 0;
        for (int i = 1; i <= 10; i++) {
            if (i % 2 != 0) {
                sum1 += i;
            }
        }
        System.out.println(sum1);
        System.out.println("--------------");

//        水仙花数  水仙花数必须是三位数   各位,是为百位数字的立方等于原数 求100-999之间的
        int a,b,c;
        for(int i=100;i<1000;i++){
            a=i/100;
            b=i%100/10;
            c=i%10;
            System.out.println(a+" "+ b+" "+c);
            if(a*a*a+b*b*b+c*c*c==a*100+b*10+c){
                System.out.println(i+"我是水仙花数");
            }
        }
    }
}
View Code

 

 

while循环

 

 

 

 

public class Demo2 {
    public static void main(String[] args) {
        //目标学会使用while循环,并理解他的使用流程
        //1 输出3次helloworld
        int i = 0;
        while (i < 3) {
            System.out.println("hello world");
            i++;
        }

        //2 实现水仙花数
        int m = 100;
        int a, b, c;
        while (m < 1000) {
            a = m / 100;
            b = m % 100 / 10;
            c = m % 10;
            if (a * a * a + b * b * b + c * c * c == a * 100 + b * 10 + c) {
                System.out.println(m + "我是水仙花数");
            }
            m++;
        }

//        3 纸张厚度0.1 毫米 折叠多少次后8848.86米
        double d = 8848860;
        double kami=0.1;
        int f = 0;
        while (kami<=d) {
            kami*=2;
            f++;
        }
        System.out.println(f+"次数"+kami);
    }
}
View Code

 

 

 

 

 

 死循环的三种循环写法

public class Demo4 {
    public static void main(String[] args) {
        //死循环 for  while  do while
//        for (; ; ) {
//            System.out.println("hello world");
//        }

        //经典写法
//        while(true){
//            System.out.println("Hello world");
//        }


        //do  while 死循环
//        do {
//            System.out.println("Hello world");
//        } while (true);
    }
}
View Code

 

死循环 模拟登录验证密码小需求

import java.util.Scanner;

public class Demo5 {
    public static void main(String[] args) {
        int pass = 520;
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名ps");
        while (true) {
            int Input = sc.nextInt();
            if (Input == pass) {
                System.out.println("登录成功");
                break;
            } else {
                System.out.println("密码错误,请重新输入密码");
            }
        }
    }
}
View Code

 

 

 continue ;break的区分

public class newDemo6 {
    public static void main(String[] args) {
        for(int i=1;i<5;i++){
            System.out.println(i);
            for(int j=1;j<5;j++){
               if(j==3){
                   System.out.println("跳出循环"+j);
                   continue;//跳过当前循环的当前次数
//                   break;//break 跳出当前这个循环(只跳出一层循环)
               }else{
                   System.out.println("没跳出"+j);
               }
            }
        }
    }
}
View Code

 

posted @ 2022-06-20 21:06  还有什么值得拥有  阅读(184)  评论(0编辑  收藏  举报