java 输入扫描仪的基本用法
最编程
2024-04-28 17:10:18
...
1.基本介绍
java.util.Scanner 是 Java5 的新特征,我们可以通过 Scanner 类来获取用户的输入,每个next获取输入对应的字符。
Scanner sc = new Scanner(System.in);
当我们通过 Scanner 类的 next() 与 nextLine() 方法获取输入的字符串,在读取前我们一般需要 使用hasNext 与 hasNextLine 判断是否还有输入的数据:
- next() -->hasNext()
- nextLine() ---->hasNextLine()
2.使用举例
hasNext和next测试
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.println("请输入:");
if (scanner.hasNext())
System.out.println("输出:"+scanner.next());
scanner.close();
}
请输入:
测试一下 哈哈
输出:测试一下
hasNextLine和nextLine
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.println("请输入:");
if (scanner.hasNextLine())
System.out.println("输出:"+scanner.nextLine());
scanner.close();
}
请输入:
测试一下 哈哈哈
输出:测试一下 哈哈哈
两者比较:
很明显,从结果来看,next获取下一个字符串,输入间隔为空格或回车时截断,而nextLine则是获取一行数据。
next():
- 一定要读取到有效字符后才可以结束输入。
- 对输入有效字符之前遇到的空白,next() 方法会自动将其去掉。
- 只有输入有效字符后才将其后面输入的空白作为分隔符或者结束符。
- next() 不能得到带有空格的字符串。
nextLine():
- 以Enter为结束符,也就是说 nextLine()方法返回输入回车之前的所有字符。
- 可以获得空白。
3.常用方式
scanner.nextLine().trim();//去掉输入两端的空格
String[] arrStr = scanner.nextLine().trim().split(" ");//将输入的一行按照空格拆分为数组
while (scanner.hasNextLine()){
int len = Integer.parseInt(scanner.nextLine().trim());
int[] arrInt = new int[len];
String[] contentStr = scanner.nextLine().trim().split(" ");
int i =0;
for (String content:contentStr){
arrInt[i++] = Integer.parseInt(content);
}
System.exit(0);
}
其他的获取输入方式:
hasNext()----next()
hasNextInt()----nextInt()
hasNextBoolean----nextBoolean()
hasNextByte()----nextByte()
hasNextShort()----nextShort()
hasNextInt()----nextInt()
hasNextLong()----nextLong()
hasNextFloat()----nextFloat()
hasNextDouble()----nextDouble()
hasNextBigInteger()----nextBigInteger()
hasNextBigDecimal()----nextBigDecimal()
4.while循环输入scanner如何退出?
以下三种退出方式:
while (!scanner.hasNext("0") ){
//标志位退出
}
while (scanner.hasNextLine() ){
// 代码段
if(scanner.hasNext("0"))//内部标志退出
break;
}
while (scanner.hasNextLine() ){
// 代码段
System.exit(0);//系统退出
}
推荐阅读
-
GreenDao 的基本用法和版本升级
-
微信 "扫一扫 "物联网,全面揭秘 "扫一扫 "背后的扫盲技术!-1.1 扫一扫感知物体是做什么的? 1.1 微信扫一扫是做什么的? 扫一扫识物是指以图片或视频(商品图片:鞋/包/美妆/服饰/家电/玩具/图书/食品/珠宝/家具/其他商品)为输入媒介,挖掘微信内容生态中的有价值信息(电商+百科+资讯,如图1所示),并展示给用户。这里的电商基本涵盖了微信小程序覆盖上亿SKU的全量优质电商,可以支持用户货比N家并直接下单购买,百科和资讯则聚合了微信内的头部自媒体如搜狗、搜搜、百度等,向用户展示和分享拍摄商品相关的内容资讯。 图 1 扫一扫识别功能示意图 欢迎大家更新iOS新版微信→扫一扫→识货,亲自体验,也欢迎大家通过识货界面的反馈按钮向我们提交反馈意见。 扫一扫识物实景图展示 1.2 扫一扫识物有哪些使用场景? 扫一扫识物的目的是为用户访问微信内部生态内容开辟一个新窗口,以用户扫图片为输入形式,为用户提供微信生态内容中的百科、资讯、电商等作为展示页面。除了用户熟悉的扫一扫操作外,我们还将进一步拓展长按操作,让用户更方便地进行扫一扫操作。"扫一扫知事 "的落地场景主要涵盖三大部分: a. 科普知识: a.科普知识。用户通过扫一扫,可以在微信生态圈中获取该对象的百科、资讯等常识或趣闻,帮助用户更好地了解该对象; b.购物场景。同样的搜索功能支持用户看到喜欢的商品立即检索到微信小程序电商中的同款商品,支持用户即扫即购; c.广告场景。扫一扫识别物体可以辅助公众号文章、视频更好地理解其中蕴含的图片信息,从而更好地投放匹配广告,提高点击率。 1.3 Sweep Sense 为 Sweep 家族带来了哪些新技术? 对于扫一扫来说,大家耳熟能详的应该就是扫一扫二维码、扫一扫小程序码、扫一扫条形码、扫一扫翻译了。无论是各种形式的编码还是文字字符,都可以看作是图片的一种特定编码形式,而物的识别则是对自然场景图片的识别,这对于扫一扫家族来说是一个质的飞跃,我们希望从物的识别入手,进一步拓展扫一扫对自然场景图片的理解能力,比如扫酒、扫车、扫植物、扫人脸等服务,如下图3所示。 图 3 Sweep 家族
-
ES]带基本操作和官方查询指导的 JAVA 集成 ES
-
Java 用户输入格式异常(InputFormatException)的解决方案
-
Java 中的键盘输入语句
-
在 Java 中输入单个字符的 2 种方法:
-
在 Java 中获取键盘输入值的三种方法
-
java 的输入语句摘要-1.使用扫描仪
-
Java 判断输入字符串是否为中文的方法摘要
-
Java 中 next 和 nextline 的用法