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!");
} }
}
}

解决方案 »

  1.   

    next() 
              查找并返回来自此扫描器的下一个完整标记。nextLine() 
              此扫描器执行当前行,并返回跳过的输入信息。JDK API 的解释
      

  2.   

    next()
    查找并返回来自此扫描器的下一个完整标记。
    nextLine()
    返回一行并且是当前行。
      

  3.   

    java.util.Scanner 5.0Scanner(InputStream in)constructs a Scanner object from the given input stream.String nextLine()reads the next line of input.String next()reads the next word of input (delimited by whitespace).int nextInt()double nextDouble()read and convert the next character sequence that represents an integer or floating-point number.boolean hasNext()tests whether there is another word in the input.boolean hasNextInt()boolean hasNextDouble()test whether the next character sequence represents an integer or floating-point number.
    以上是core Java 中的权威解释~!
    楼主可能和我一样是初学者!
    建议查查API,要是jdk1.5版本以上的,否则找不到关于scanner的信息。
    有查询功能的那种!网上有下载的,用起来很方便!good luck!!!
      

  4.   

    next()
    查找并返回来自此扫描器的下一个完整标记。
    nextLine()
    返回一行并且是当前行。
      

  5.   


    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 的文档注释,可以的话自己理解吧。
      

  6.   

    简单的说nextLine() 返回的是一行。而next() 返回的只是第一个输入。
    比如;输入hello java
    nextLine() 读的是hello java
    next() 读的是hello 
      

  7.   

    我以7#的输入为例子,如果你输入hello java(回车),
    那nextLine输出的是hello java,不包括回车
    next输出的则是hello  因为有一个空格
      

  8.   

    又仔细看了下楼主的代码:可以将“String s=sc.next();”换成“String s=sc.nextLine();”但是位置要换一下,import java.util.*; 
    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!"); 
    } } 

    } 请大侠们指点一下迷津吧!
      

  9.   

    “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());
       
       }
    }
      

  10.   


    可能是API上说明没理解好
      

  11.   

    不是API的问题 
    谁能帮解释下代码? 关注ing
      

  12.   


    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();
      

  13.   

    这位的分析是正确的!
    楼主可以在开头设置断点,然后用debug调试,结果很明朗!
      

  14.   

    next呢是到空格就结束
    nextLine是到换行才结束,就这么点区别哈
      

  15.   

    nextInt()和nextLine()最好不要同时使用,因为nextInt()只读取一个整数,会留下一个换行符,而nextLine()从上次信息开始读,读完换行符就以为按下空格了 就输入了。
    最好只用nextLine(),然后用Integer.parseInt("字符串"),把读到的字符串转化成int型
      

  16.   

    看了这么多人的回复,感觉只有#11楼的解答是最正确的。大多数人可能根本就没明白问题在哪里,包括照抄API的那些人。18楼的解释也接近了,但是有一个问题是,楼主的情况跟println没有关系。即便把println换成print也一样会出错。
      

  17.   

    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!");
    }}
    }
    }
      

  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!");
    }}
    }
    }
      

  19.   

    23楼正解,nextLine()读入的是nextInt()解析后遗留的换行符,LZ第二次再输入任何字符串,实际上nextLine()进行解析的是“\r\nxxxx”,再对照API的解释,应该比较好理解了。