java06-进阶Scanner-顺序结构--选择结构(单选择+双选择+嵌套if结构)

顺序结构

package com.guan.struct;

public class XunHuanDome {
    public static void main(String[] args) {
        System.out.println("hello1");
        System.out.println("hello2");
        System.out.println("hello3");
        System.out.println("hello4");

    }
}
hello1
hello2
hello3
hello4

选择结构

单选择结构

public static void main(String[] args) {
        //分数大于60就是及格,反之不及格
        Scanner scanner = new Scanner(System.in);

        System.out.println("请输入成绩:");
        int score = scanner.nextInt();

        if (score>60){
            System.out.println("及格");

        }else{
            System.out.println("不及格");

        }
        scanner.close();
    }

双选择结构

public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入成绩:");
        int score = scanner.nextInt();

        if (score<60 && score>=0){
            System.out.println("不及格!");
        }else if (score==60){
            System.out.println("及格!");
        } else if (score>=60 &&  score<80){
            System.out.println("良好!");
        }else if (score>=80 && score<100){
            System.out.println("优秀!");
        }else if(score==100){
            System.out.println("满分!");
        }else{
            System.out.println("输入不合法!");
        }

        scanner.close();



    }

嵌套if结构

public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);


        System.out.println("输入3个数字:");
        int x = scanner.nextInt();

        int y = scanner.nextInt();

        int z = scanner.nextInt();
  
        if (x > y) {
            if (x > z) {
                System.out.println("mac=" + x);
            } else {
                System.out.println("max=" + z);
            }

        } else if (y > z) {
            System.out.println("max=" + y);
        } else {
            System.out.println("max=" + z);
        }

        scanner.close();
}

posted @ 2022-08-03 22:15  干饭人减肥魂  阅读(50)  评论(0)    收藏  举报