• Java选择结构


    package com.xiaojie.struct;
    
    import java.util.Scanner;
    
    public class IfDemo01 {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
    
            System.out.println("请输入内容: ");
            String s = scanner.nextLine();
    
            if (s.equals("Hello")) {
                System.out.println(s);
            }
    
            System.out.println("End");
            scanner.close();
    
        }
    }
    

    package com.xiaojie.struct;
    
    import java.util.Scanner;
    
    public class IfDemo02 {
        public static void main(String[] args) {
            //考试分数大于60分数就是及格,小于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();
    
        }
    }
    

    package com.xiaojie.struct;
    
    import java.util.Scanner;
    
    public class IfDemo03 {
        public static void main(String[] args) {
            //考试分数大于60分数就是及格,小于60分数就是不及格。
    
            Scanner scanner = new Scanner(System.in);
    
            /*
            if 语句至多有 1 个 else 语句,else 语句在所有 else if 语句之后。
            if 语句可以有若干个 else if 语句,它们必须在 else 语句之前。
            一旦其中一个 else if 语句检测为true,其他的 else if 以及 else 语句都将跳过执行。
             */
    
            System.out.println("请输入成绩: ");
            int score = scanner.nextInt();
    
            if (score==100){
                System.out.println("恭喜满分");
            }else if (score<100 && score>=90){
                System.out.println("A级");
            }else if (score<90 && score>=80){
                System.out.println("B级");
            }else if (score<80 && score>=70){
                System.out.println("C级");
            }else if (score<70 && score>=60){
                System.out.println("D级");
            }else if (score<60 && score>=0){
                System.out.println("不及格");
            }else {
                System.out.println("成绩不合法");
            }
            scanner.close();
        }
    }
    

    package com.xiaojie.struct;
    
    public class SwitchDemo01 {
        public static void main(String[] args) {
            //case穿透    //switch 匹配一个具体的值
            char grade = 'B';
    
            switch (grade){
                case 'A':
                    System.out.println("优秀");
                    break; //可选
                case 'B':
                    System.out.println("良好");
                    break;
                case 'C':
                    System.out.println("及格");
                    break;
                case 'D':
                    System.out.println("再接再厉");
                    break;
                case 'E':
                    System.out.println("挂科");
                    break;
                default:
                    System.out.println("未知等级");
                    break;
            }
        }
    }
    
    package com.xiaojie.struct;
    
    public class SwitchDemo02 {
        public static void main(String[] args) {
            String name = "tom";
            //JDK7的新特性,表达式结果可以是字符串!!!
            //字符串的本质还是数字
    
            //反编译   java---class(字节码文件)---反编译(IDEA)
    
            switch (name){
                case "tom":
                    System.out.println("tom");
                    break;
                case "jerry":
                    System.out.println("jerry");
                    break;
                default:
                    System.out.println("Incorrect");
                    break;
            }
        }
    }
    
  • 相关阅读:
    第一章:绪论
    第二章 算法入门
    java基本语法特殊点
    css学习の第六弹—样式设置小技巧
    122. Best Time to Buy and Sell Stock II--easy
    121. Best Time to Buy and Sell Stock--easy
    Weekly Contest 129--1023. Binary String With Substrings Representing 1 To N--Medium
    Weekly Contest 129--1021. Best Sightseeing Pair--Medium
    Weekly Contest 129--1022. Smallest Integer Divisible by K--Medium
    Weekly Contest 129--1020. Partition Array Into Three Parts With Equal Sum--easy
  • 原文地址:https://www.cnblogs.com/Notesdata/p/14063498.html
Copyright © 2020-2023  润新知