Java流程控制1(Scanner 顺序结构,选择结构)

时间:2021-02-19 13:46:48   收藏:0   阅读:0

Java流程控制

用户交互Scanner

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

        //创建一个扫描器对象,用于接受键盘数据
        Scanner scanner = new Scanner(System.in);

        System.out.println("使用next方法接收:");//程序与等待用户输入

        //判断用户有没有输入字符串
        if (scanner.hasNext()){
            //使用next方法接收
            String str = scanner.next();
            System.out.println("输入的内容为:"+ str);
            //如果输入的内容有空格,会中断内容,只输出空格前的内容

        }

        scanner.close();//凡是IO流的类如果不关闭会一直占用资源,要养成好习惯用完就关闭


    }
hasNextLine()
    public static void main(String[] args) {
        //从键盘接受数据
        Scanner scanner = new Scanner(System.in);//创建了等号右边,快速创建左边  Ctrl+Alt+V

        System.out.println("使用nextLine方法接收:");

        //判断是否还有输入
        if (scanner.hasNextLine()){
            String str = scanner.nextLine();
            System.out.println("输出的内容为:"+str);
            //此时有空格不会被打断
        }

        scanner.close();

    }
   
            //从键盘接收数据
        int i = 0;
        float f = 0.0f;

        System.out.println("请输入整数:");

        //如果...那么
        if (scanner.hasNextInt()){
            i = scanner.nextInt();
            System.out.println("整数数据:"+i);
        }else{
            System.out.println("输入的不是整数数据!");
        }

        System.out.println("请输入小数:");

        //如果...那么
        if (scanner.hasNextFloat()){
            f = scanner.nextFloat();
            System.out.println("小数数据:"+f);
        }else{
            System.out.println("输入的不是小数数据!");
        }

        
        scanner.close();

顺序结构

选择结构

if(布尔表达式){
    //如果布尔表达式为true将执行语句
}

Scanner scanner = new Scanner(System.in);

        System.out.println("请输入内容:");
        String s = scanner.nextLine();

        //equals:判断字符串是否相等
        if (s.equals("Hello")){
            System.out.println(s);
        }

        System.out.println("End");
        
        scanner.close();
if(布尔表达式){
    //如果布尔表达式的值为true
}else{
    //如果布尔表达式的值为false
}

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();
if(布尔表达式1){
    //如果布尔表达式1的值为true执行代码
}else if(布尔表达式2){
    //如果布尔表达式2的值为true执行代码
}else if(布尔表达式3){
    //如果布尔表达式3的值为true执行代码
}else {
    //如果以上布尔表达式都不为true执行代码
}

 /*
        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();
if (布尔表达式1){
    //如果布尔表达式1为true 执行代码
    if(布尔表达式2){
        //如果布尔表达式2为true 执行代码
    }
}
switch(expression){
    case value:
        //语句
        break;//可选
    case value:
        //可选
        break;//可选
        //你可以有任意数量的case语句
    default://可选
        //语句
}

//case穿透        switch匹配一个具体的值
        char grade = ‘C‘;

        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;//可选
评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!