2014-06-12 103 views
0
import java.util.Scanner; 
public class test { 

public static void main(String[] args) 
{ 
    Scanner input = new Scanner (System.in); 
    boolean US1 = false; 
    boolean game; 
    int score = 1; 
    int wage = 0; 
    int fin_score = 0; 
    String ans; 

    if (US1 == false) 
    { 
     game = false; 
     System.out.println (score); 
     System.out.println("Enter a wager"); 
     wage = input.nextInt(); 
    } 

    if (wage < score) 
    { 
     System.out.println ("What is the capital of Liberia?"); 
     ans = input.nextLine(); 

     if (ans.equalsIgnoreCase("Monrovia")) 
     { 
      System.out.println ("You got it right!"); 
      System.out.println ("Final score " + fin_score); 
     } 
    } 
    // TODO Auto-generated method stub 
} 
} 

由于某些原因,一旦我输入工资值,代码将打印出“利比里亚的首都是什么?”。然后终止。林不知道是怎么回事,为什么扫描仪不会让我输入的问题扫描仪停止阅读输入

+0

'Scanner'?我没有在您提供的代码中看到“Scanner”。 – awksp

+0

声明“输入”在哪里? – Michelle

+0

[这是一个已知的问题](http://stackoverflow.com/questions/7056749/scanner-issue-when-using-nextline-after-nextxxx) – gtgaxiola

回答

0

问题的答案是,扫描仪nextInt不消耗输入

所以,你必须使用一个nextLine()

添加以下内容:右后

input.nextLine();

wage = input.nextInt();