控制语句

2020-04-15  本文已影响0人  雪上霜

java.util.Scanner s = new java.util.Scanner(System.in);

控制语句:选择语句、循环语句、转向语句。

public class IfTest{
    public static void main(String[] args){
        //if中表达式是布尔表达式
        boolean sex = true;
        if(sex){
            System.out.println("男");
        }else {
            System.out.println("女");
        }
        
        java.util.Scanner s = new java.util.Scanner(System.in);
        
        int nianLing = s.nextInt();
        
        if(nianLing < 5){
            System.out.println("幼儿");
        }else if(nianLing < 10){
            System.out.println("少儿");
        }else if(nianLing < 18){
            System.out.println("少年");
        }else if(nianLing < 35){
            System.out.println("青年");
        }else if(nianLing < 55){
            System.out.println("中年");
        }else{
            System.out.println("老年");
        }
        
        java.util.Scanner ss = new java.util.Scanner(System.in);
        
        System.out.println("请输入您的考试成绩:");
        
        double score = ss.nextDouble();
        String str = "优";
        if(score > 100){
            System.out.println("输出成绩不合法");
            return;
        }else if(score <= 60){
            str = "不及格";
        }else if(score <= 70){
            str = "及格";
        }else if(score <= 80){
            str = "中";
        }else if(score <= 90){
            str = "良";
        }else if(score <= 100){
            str = "优";
        }
        System.out.println(str);
        
        //switch值在JDK8之前不支持String,只支持int,之后支持String和int
        //byte,short,char也可以支持在switch表达式中,会进行自动转换
        long x = 100L;
        switch(x){  //类型不兼容
             
        }
        byte b = 100;
        switch(b){
        
        }
        short ss = 100;
        switch(ss){
        
        }
        
        char c = 'a';
        switch(c){
        
        }
        String str = "aa";
        switch(str){
        
        }
        
        System.out.println("请输入0-6的整数:");
        int num = s.nextInt();
        switch(num){
        case 0:
            System.out.println("星期日");
            break;
        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合并
        switch(num){
        case 0: case 1:case 2: case 3:
            System.out.println("星期日");
            break;
        case 4: case 5: case 6:
            System.out.println("星期四");
            break;
        }

    }
}
public class IfTest{
    public static void main(String[] args){
        java.util.Scanner s = new java.util.Scanner(System.in);
        
        System.out.println("请输入您的性别:");
        int gender = s.nextInt();
        
        System.out.print("请输入天气:");
        int weather = s.nextInt();
    
        if(weather == 1){
            System.out.println("雨天");
        }else if(weather == 0){
            System.out.println("晴天");
        }
        
        if(gender == 1){
            System.out.println("男");
        }else if(gender == 0){
            System.out.println("女");
        }
    }
}
public class Switch{
    public static void main(String[] args){
        java.util.Scanner s = new java.util.Scanner(System.in);
        
        System.out.println("请输入学生成绩");
        double score = s.nextDouble();
        
        int grade = (int)(score/10);
        String str = "不及格";
        switch(garde){
        case 0: case 1: case 2: case 3: case 4: case 5:
            str = "不及格";
            break;
        case 6:
            str = "及格";
            break;
        case 7:
            str = "中";
            break;
        case 8:
            str = "良";
            break;
        case 9: case 10:
            str = "优";
            break;
        }
        
        System.out.println(str);
    }
}
上一篇下一篇

猜你喜欢

热点阅读