Scanner中next()与nextLine()的区别?

mengbingqingcui 2009-05-13 09:46:00
import java.util.*;
class NextNextLine{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
while(true){
System.out.println("input the first number:");
int a=sc.nextInt();
if(a==-1){
System.out.println("over!");
break;
}
System.out.println("input the operator:");
String s=sc.next();//String s=sc.nextLine()//为什么这个地方用nextLine()就不行了呢?
System.out.println("input the second number:");
int b=sc.nextInt();
if(s.equals("+")){
System.out.println(a+"+"+b+"="+(a+b));
}else if(s.equals("-")){
System.out.println(a+"-"+b+"="+(a-b));
}else if(s.equals("*")){
System.out.println(a+"*"+b+"="+(a*b));
}else if(s.equals("/")){
System.out.println(a+"/"+b+"="+(double)(a/b));
}else{
System.out.println("false!");
}

}
}
}
...全文
2566 30 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
30 条回复
切换为时间正序
请发表友善的回复…
发表回复
hahuhi 2011-11-09
  • 打赏
  • 举报
回复
23楼正解,nextLine()读入的是nextInt()解析后遗留的换行符,LZ第二次再输入任何字符串,实际上nextLine()进行解析的是“\r\nxxxx”,再对照API的解释,应该比较好理解了。
yyz420911451 2011-10-18
  • 打赏
  • 举报
回复
这样改就好了:
import java.util.*;
class NextNextLine{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
while(true){
System.out.println("input the first number:");
String m=sc.nextLine();
int a=Integer.parseInt(m);
if(a==-1){
System.out.println("over!");
break;
}
System.out.println("input the operator:");

String s=sc.nextLine();//String s=sc.nextLine()//为什么这个地方用nextLine()就不行了呢?

System.out.println("input the second number:");
String n=sc.nextLine();
int b=Integer.parseInt(n);

if(s.equals("+")){
System.out.println(a+"+"+b+"="+(a+b));
}else if(s.equals("-")){
System.out.println(a+"-"+b+"="+(a-b));
}else if(s.equals("*")){
System.out.println(a+"*"+b+"="+(a*b));
}else if(s.equals("/")){
System.out.println(a+"/"+b+"="+(double)(a/b));
}else{
System.out.println("false!");
}

}
}
}
yyz420911451 2011-10-18
  • 打赏
  • 举报
回复
import java.util.*;
class NextNextLine{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
while(true){
System.out.println("input the first number:");
String m=sc.nextLine();
int a=Integer.parseInt(m);
if(a==-1){
System.out.println("over!");
break;
}
System.out.println("input the operator:");

String s=sc.nextLine();//String s=sc.nextLine()//为什么这个地方用nextLine()就不行了呢?

System.out.println("input the second number:");
String n=sc.nextLine();
int b=Integer.parseInt(n);

if(s.equals("+")){
System.out.println(a+"+"+b+"="+(a+b));
}else if(s.equals("-")){
System.out.println(a+"-"+b+"="+(a-b));
}else if(s.equals("*")){
System.out.println(a+"*"+b+"="+(a*b));
}else if(s.equals("/")){
System.out.println(a+"/"+b+"="+(double)(a/b));
}else{
System.out.println("false!");
}

}
}
}
yapai 2011-08-10
  • 打赏
  • 举报
回复
我也遇到了和楼主同样的问题,此贴的确很好,学习了。同意LS的看法
arkonica 2011-02-19
  • 打赏
  • 举报
回复
看了这么多人的回复,感觉只有#11楼的解答是最正确的。

大多数人可能根本就没明白问题在哪里,包括照抄API的那些人。

18楼的解释也接近了,但是有一个问题是,楼主的情况跟println没有关系。即便把println换成print也一样会出错。
lsg19890214 2010-10-05
  • 打赏
  • 举报
回复
学习了!!哈哈
kahnnash 2010-09-02
  • 打赏
  • 举报
回复
nextInt()和nextLine()最好不要同时使用,因为nextInt()只读取一个整数,会留下一个换行符,而nextLine()从上次信息开始读,读完换行符就以为按下空格了 就输入了。
最好只用nextLine(),然后用Integer.parseInt("字符串"),把读到的字符串转化成int型
macrotea-cn 2010-09-01
  • 打赏
  • 举报
回复
此贴很有用,留下脚印,将来方便查询!!
cyd411 2010-09-01
  • 打赏
  • 举报
回复
此贴很有用,留下脚印,将来方便查询
mf0606 2010-04-24
  • 打赏
  • 举报
回复
next呢是到空格就结束
nextLine是到换行才结束,就这么点区别哈
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 zfq642773391 的回复:]
Java code

System.out.println("input the operator:");
String s=sc.next();//String s=sc.nextLine()//为什么这个地方用nextLine()就不行了呢?



并不是nextLIne()不行,因为此方法会继续在输入信息中查找行分隔符,所以如果没有行分隔符,它可能会缓冲所有输入信息,并查找……
[/Quote]


这位的分析是正确的!


楼主可以在开头设置断点,然后用debug调试,结果很明朗!
zfq642773391 2010-04-24
  • 打赏
  • 举报
回复

System.out.println("input the operator:");
String s=sc.next();//String s=sc.nextLine()//为什么这个地方用nextLine()就不行了呢?


并不是nextLIne()不行,因为此方法会继续在输入信息中查找行分隔符,所以如果没有行分隔符,它可能会缓冲所有输入信息,并查找要跳过的行。 你上一句是println()最后有一个换行符,你nextLine时,获得的是这个分隔符
如果你在nextLine()之前用next()来获得这个换行符,再用nextLIne()来获取一行就行了

sc.next();
String s=sc.nextLine();


cllover 2010-04-24
  • 打赏
  • 举报
回复
up,学些了
dajiadebeibei9 2009-05-28
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 justinavril 的回复:]
我以7#的输入为例子,如果你输入hello java(回车),
那nextLine输出的是hello java,不包括回车
next输出的则是hello  因为有一个空格
[/Quote]

说的很正确
zhangjingrui 2009-05-28
  • 打赏
  • 举报
回复
进来学习学习。
孝东 2009-05-27
  • 打赏
  • 举报
回复
进来学习
Adebayor 2009-05-27
  • 打赏
  • 举报
回复
[Quote=引用楼主 mengbingqingcui 的帖子:]
import java.util.*;
class NextNextLine{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
while(true){
System.out.println("input the first number:");
int a=sc.nextInt();
if(a==-1){
System.out.println("over!");
                                break;
                        }
System.out.println("input the operator:");
String s=sc.next();//String s=sc.nextLine()…
[/Quote]
不是API的问题
谁能帮解释下代码? 关注ing
JayPan2008 2009-05-27
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 skfeng36 的回复:]
简单的说nextLine() 返回的是一行。而next() 返回的只是第一个输入。
比如;输入hello java
nextLine() 读的是hello java
next() 读的是hello
[/Quote]

可能是API上说明没理解好
  • 打赏
  • 举报
回复
简单的说nextLine() 返回的是一行。而next() 返回的只是第一个输入。
比如;输入hello java
nextLine() 读的是hello java
next() 读的是hello
lulu0126 2009-05-14
  • 打赏
  • 举报
回复
“String s=sc.next();//String s=sc.nextLine()//为什么这个地方用nextLine()就不行了呢? ”

不行的原因为:
上面执行过“int a=sc.nextInt(); ”

再执行“String s=sc.nextLine();//”

此时s中已经没有值了,如果用"System.out.println(s.length());"输出s的长度,你会发现,s的长度为“0”

下面测试代码说明了这个问题,仅供参考:

import java.util.Scanner;
public class code
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);



int myint = sc.nextInt();
System.out.println("sc.nextInt()"+myint);

System.out.println("------------------------------");

String str2="qweqwe";
System.out.println("str2=sc.nextLine();执行前,str2的值:"+str2);
str2=sc.nextLine();
System.out.println("str2=sc.nextLine();执行后,str2的值:"+str2);
System.out.println("str2.length()为"+str2.length());

}
}



加载更多回复(9)

62,635

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧