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!");
} }
}
}
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!");
} }
}
}
查找并返回来自此扫描器的下一个完整标记。nextLine()
此扫描器执行当前行,并返回跳过的输入信息。JDK API 的解释
查找并返回来自此扫描器的下一个完整标记。
nextLine()
返回一行并且是当前行。
以上是core Java 中的权威解释~!
楼主可能和我一样是初学者!
建议查查API,要是jdk1.5版本以上的,否则找不到关于scanner的信息。
有查询功能的那种!网上有下载的,用起来很方便!good luck!!!
查找并返回来自此扫描器的下一个完整标记。
nextLine()
返回一行并且是当前行。
public String nextLine()
Advances this scanner past the current line and returns the input that was skipped. This method returns the rest of the current line, excluding any line separator at the end. The position is set to the beginning of the next line.
Since this method continues to search through the input looking for a line separator, it may buffer all of the input searching for the line to skip if no line separators are present. Returns:
the line that was skipped
public String next()
Finds and returns the next complete token from this scanner. A complete token is preceded and followed by input that matches the delimiter pattern. This method may block while waiting for input to scan, even if a previous invocation of hasNext() returned true. Specified by:
next in interface Iterator<String>
Returns:
the next token
这是api 的文档注释,可以的话自己理解吧。
比如;输入hello java
nextLine() 读的是hello java
next() 读的是hello
那nextLine输出的是hello java,不包括回车
next输出的则是hello 因为有一个空格
class NextNextLine{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
while(true){
System.out.println("input the operator:");
String s=sc.nextLine();//将“String s=sc.next();”换成“String s=sc.nextLine();”
System.out.println("input the first number:");
int a=sc.nextInt();
if(a==-1){
System.out.println("over!");
break;
} 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!");
} }
}
} 请大侠们指点一下迷津吧!
上面执行过“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());
}
}
可能是API上说明没理解好
谁能帮解释下代码? 关注ing
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();
楼主可以在开头设置断点,然后用debug调试,结果很明朗!
nextLine是到换行才结束,就这么点区别哈
最好只用nextLine(),然后用Integer.parseInt("字符串"),把读到的字符串转化成int型
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!");
}}
}
}
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!");
}}
}
}