Scanner对象运算

//创建Scanner对象
Scanner scanner = new Scanner(System.in);

//判断键盘是否输入字符
if (scanner.hasNext()) {
String str = scanner.next();
//如果输入为hello world ,这会显示hello,因为有空格,next会直接截断
}
//判断是否是否还有输入
if (scanner.hasNextLine()) {
String str1 = scanner.nextLine();
//如果输入为hello world ,这会显示hello world
}
//凡是io流操作的对象用掉之后都需要关闭,否则会继续占用资源
scanner.close();

posted @ 2021-03-17 15:06  HOTCOLD  阅读(106)  评论(0)    收藏  举报