【狂神说Java】用户交互Scanner

时间:2021-09-01 13:39:02   收藏:0   阅读:17

Scanner类来获取用户的输入

java.util.Scanner 是Java5的新特征,我们可以通过Scanner类来获取用户的输入。

Scanner s = new Scanner(System.in);
// next()
// 创建一个扫描器对象,用于接收键盘数据
Scanner s = new Scanner(System.in);
System.out.println("使用next方式接收:");
//判断用户有没有输入字符串
if (s.hasNext()){
    //用next方式接收
    String str = s.next();
    System.out.println(str);
}
//凡是属于IO流的类如果不关闭会一直占用资源
s.close();
// nextLine()
// 创建一个扫描器对象,用于接收键盘数据
Scanner s = new Scanner(System.in);
System.out.println("使用nextLine方式接收:");
//判断用户有没有输入字符串

String str = s.nextLine();
System.out.println(str);
}
//凡是属于IO流的类如果不关闭会一直占用资源
s.close();

原文:https://www.cnblogs.com/jie7/p/15211969.html

评论(0)
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!