Java学习三,选择控制和循环控制

这节主要的学习内容:(选择控释)1.if结构;2.switch结构;(循环选择)3.while循环;4.do-while循环;5.for循环6.break语句;7.continue语句

1.if结构

   1.1 多重if else语句

package com_imooc;

import java.util.Scanner;

public class Huozhe {
    public static void main(String[] vvv) {
        /*
            90-100 优
            80-90  良
            60-80  中
            <60    不及格
         */
        System.out.print("请输入你得成绩: ");
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        if (n >= 90) {
            System.out.println("优");
        } else if (80 <= n && n < 90) {
            System.out.println("良");
        }else if (60 <= n && n <80){
            System.out.println("中");
        }else if (n < 60){
            System.out.println("不及格");
        }

    }
}

    1.2 嵌套if语句

package com_imooc;

public class Huozhe {
    public static void main(String[] vvv) {

        //判断int aa,bb的大小关系
        int aa = 5, bb = 6;
        if (aa != bb) {
            if (aa > bb){
                System.out.println("aa大于bb");
            }else {
                System.out.println("bb大于aa");
            }
        } else {
            System.out.println("aa等于bb");
        }
    }
}

2.switch结构

    

package com_imooc;

import java.util.Scanner;

public class Huozhe {
    public static void main(String[] vvv) {

        //键盘输入1-7,对应出礼拜一到日
        System.out.print("请输入小数字1-7: ");
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        switch (n) {
            case 1:
                System.out.println("礼拜一");
                break;
            case 2:
                System.out.println("礼拜二");
                break;
            case 3:
                System.out.println("礼拜三");
                break;
            case 4:
                System.out.println("礼拜四");
                break;
            case 5:
                System.out.println("礼拜五");
                break;
            case 6:
                System.out.println("礼拜六");
                break;
            case 7:
                System.out.println("礼拜日");
                break;
            default:
                System.out.println("你超出去1-7的范围了");
        }
    }
}

3.while循环

   基本语法

          

package com_imooc;

public class Xunhuan {
    public static void main(String[] vvv) {
        //案例一,将小于5的数字输出
        int n = 0;
        while (n < 5) {
            System.out.println(n);
            n++;
        }

        //案例二,求1到5的累加和
        int count = 0;
        while (n <= 5) {
            count += n;
            if (n == 5) {
                System.out.println("最后的和是:" + count);
            }
            n++;
        }

        //案例三,循环26个字母,分两行输出
        char ch = 'a';
        int sum = 1;
        while (ch <= 'z') {
            System.out.print(ch + " ");
            if (sum == 13){
                System.out.println();
            }
            ch++;
            sum++;
        }
    }
}

4.do-while循环 

                     基本语法

                                

                             

 

package com_imooc;

import java.util.Scanner;

public class Xunhuan {
    public static void main(String[] vvv) {

        //do-while
        //案例一,将小于5的数字输出
        int m = 0;
        do {
            System.out.println(m);
            m++;
        } while (m < 5);

        //案例二,求1到5的累加和
        int do_m=1;
        int do_count=0;
        do {
            do_count+=do_m;
            if (do_m == 5){
                System.out.println("和是:"+do_count);
            }
            do_m++;
        }while (do_m < 6);

        //案例三,猜数字
        int x = 5;
        boolean xx = true;
        do {
            System.out.print("请输入一个整数: ");
            Scanner s = new Scanner(System.in);
            int n = s.nextInt();
            if (n > 5) {
                System.out.println("大了");
            } else if (n < 5) {
                System.out.println("小了");
            } else if (n == 5) {
                System.out.println("对了");
                xx = false;
            }
        } while (xx);
    }
}

5.for循环

    基本语法

            

package com_imooc;

import java.util.Scanner;

public class Xunhuan {
    public static void main(String[] vvv) {

        //for
        //案例一,求1到5的累加和
        int for_count = 0;
        for (int a = 0; a <= 5; a++) {
            for_count += a;
            if (a == 5) {
                System.out.println("和是:" + for_count);
            }
        }

        //案例二,1!+2!+3!+4!+...+10!
        long jia = 0,cheng = 1;
        for (int n = 1; n <= 10; n++) {
            for (int m = 1; m <= n; m++) {
                cheng*=m;
            }
            jia+=cheng;
            cheng=1;
        }
        System.out.println(jia);
    }
}

6.break语句

                  

                  

package com_imooc;

import java.util.Scanner;

public class Xunhuan {
    public static void main(String[] vvv) {
        //break
        //案例一,输出0时,退出循环
        while (true) {
            System.out.print("请输入一个整数: ");
            Scanner s = new Scanner(System.in);
            int n = s.nextInt();
            if (n == 0) break;
        }
    }
}

7.continue语句

     作用是:

        

    例题:

                      

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值