Java流控制---键盘输入、顺序结构与选择结构

一、Scanner的使用

java.util.Scanner是Java5的新特征,我们可以通过Scanner类来获取用户的输入
定义Scanner的基本语法

Scanner scanner = new Scanner(System.in);
  1. scanner.next():一定要读到有效字符之后才会结束输入,对于输入有效字符之前的空白,scanner.next()会将其去掉,只有输入有效字符后才将其后面输入的空白作为分隔符或者结束符,scanner.next()不能得到带有空格的字符串
  2. scanner.nextLine():以回车作为结束符,scanner.nextLine()方法返回输入的回车之前的所有字符,可以获得空白。
  3. 在结束使用的时候需要使用scanner.close()方法释放资源。

二、顺序结构

我们程序执行通常都是按照顺序结构一步一步的向下执行代码的。

三、if与switch选择结构

if单选择结构

public class HelloWorld {
    public static void main(String[] args) {
        boolean flag = true;
        if (flag) {
            System.out.println("akita");
        }
    }
}

if双选择结构

public class HelloWorld {
    public static void main(String[] args) {
        boolean flag = true;
        if (flag) {
            System.out.println("akita");
        }else {
            System.out.println("lee");
        }
    }
}

if多选择结构

public class HelloWorld {
    public static void main(String[] args) {
        int num = 89;
        if (num >= 90 && num < 100) {
            System.out.println("优秀");
        } else if (num >= 80 && num < 90) {
            System.out.println("良");
        } else if (num >= 70 && num < 80) {
            System.out.println("一般");
        } else if (num >= 60 && num < 70) {
            System.out.println("还行");
        } else if (num >= 0 && num < 60) {
            System.out.println("不及格");
        } else {
            System.out.println("错误成绩");
        }
    }
}

嵌套的if结构

public class HelloWorld {
    public static void main(String[] args) {
        int num = 89;
        int score = 90;
        if (num < 100) {
            if (score == 90) {
                System.out.println("akita!");
            }
        }
    }
}

switch多选择结构

public class HelloWorld {
    public static void main(String[] args) {
        int num = 90;
        switch (num) {
            case 100:
                System.out.println("100");
                break;
            case 90:
                System.out.println("90");
                break;
            case 80:
                System.out.println("80");
                break;
            default:
                System.out.println("未找到");
        }
    }
}
public class HelloWorld {
    public static void main(String[] args) {
        int num = 90;
        switch (num) {
            case 100:
                System.out.println("100");
                break;
            case 90:
                System.out.println("90");
                break;
            case 80:
                System.out.println("80");
                break;
            default:
                System.out.println("未找到");
        }
    }
}

记得需要在每个case后面写上break语句,否则会造成逻辑错误,如以下代码

public class HelloWorld {
    public static void main(String[] args) {
        int num = 90;
        switch (num) {
            case 100:
                System.out.println("100");
                break;
            case 90:
                System.out.println("90");
            case 80:
                System.out.println("80");
                break;
            default:
                System.out.println("未找到");
        }
    }
}

此代码输出如下:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

水哥很水

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值