2015-05-09 55 views
2
package Lessons; 

import java.util.Scanner; 

public class Math { 
    public static void main(String[] args) { 
     Scanner s = new Scanner(System.in); 
     System.out.print("please enter the first number of your question! "); 
     int a1 = s.nextInt(); 
     System.out.print("So you chose " + a1); 
     System.out.println(" Choose ur second number"); 
     int a2 = s.nextInt(); 
     System.out.print("You chose " + a2 + " for your second number"); 
     System.out.print(" Now enter ur operator "); 
     String b1 = s.nextLine(); 
     if (b1.equalsIgnoreCase("-")) { 
      System.out.println("You chose " + b1 + "as ur operator"); 
     } 
    } 

} 

我不明白为什么15行不采取任何输入,任何帮助,将不胜感激:3为什么我的Java扫描仪不能输入?

输出

请输入您的问题的第一个数字! 2552所以,你选择了2552 选择乌尔第二个数字41,您为您的第二号选择41现在 进入乌尔操作

输出端,并在最后一行停止出于某种原因,并没有采取任何信息!

+0

您可以发布您的输出吗? – JavaFanatic

回答

3

您需要呼叫in.nextLine()紧接您拨打的电话号码in.nextInt()之后。原因是只要求下一个整数不会消耗输入中的整个行,因此您需要跳到下一个新行通过调用in.nextLine()输入字符。

int a2 = s.nextInt(); 
s.nextLine(); 

这几乎是每一个有你需要调用一个不消耗整条生产线的方法,后得到一个新的行时间来完成,例如,当你调用nextBoolean()

+0

即时通讯做一个字符串即时通讯,试图采取不是一个int :) – chandler11able

+0

@Phoenix请重新阅读我的答案,这是你的问题的解决方案。 – SamTebbs33

+0

哦,对不起!你是对的!感谢m8! :D – chandler11able

1

调用s.nextInt()为读取下一个int,但之后不读取新的行字符。

... 
System.out.print(" Now enter ur operator "); 
s.nextLine(); 
String b1 = s.nextLine(); 
... 
0

试试这个:所以新线将上线15如果你是确保每一个输入将被放在一个单独的线被读取,那么你可以通过把一个额外的s.nextLine()线15前解决这个问题用于输入字符串的部分。

`System.out.print(" Now enter ur operator "); 
    String b1 =s.next(); 
    if (b1.equalsIgnoreCase("-")) { 
     System.out.println("You chose " + b1 + "as ur operator"); 
}`