2017-05-07 89 views
0
for (int i = 1; i <= value; i++) { 
     System.out.print("Player " + i + ", Please Enter your name : "); 
     String PlayerName = input.nextLine(); 
     System.out.print("Please enter a number between 0 and 9 : "); 
     int PlayerNumber = input.nextInt(); 
     System.out.println(PlayerName + " : " + PlayerNumber); 
    } 

输出:Player 1, Please Enter your name : Please enter a number between 0 and 9 :如何让我的nextLine()等待用户的输入?

如果我改变input.nextLineinput.next,它会等待用户输入第一个,但他/她将无法写出自己的全名。

我的问题:我可以改变什么,以便用户可以先输入他的全名,然后输入他的号码?

+0

的IT可以从点列表做到这一点:新的经纬度(points.get(I).latitude,points.get(I).lontitude)? –

+2

顺便说一句,请不要大写你的变量名 –

+0

我试过“重复问题”的方法,但它仍然不起作用。如果我在'String PlayerName = input.nextLine();'的下面放置一个'input.nextLine();',我的输出变成'[blank]:PlayerNumber' – Crypto

回答

1

消耗换行符input.nextInt()

for (int i = 1; i <= value; i++) { 
    input.nextLine(); //add this to top 
    System.out.print("Player " + i + ", Please Enter your name : "); 
    String PlayerName = input.nextLine(); 
    System.out.print("Please enter a number between 0 and 9 : "); 
    int PlayerNumber = input.nextInt(); 

    System.out.println(PlayerName + " : " + PlayerNumber); 
} 
+0

我得到相同的输出,但是如果我移动在'String PlayerName = input.nextLine();'下面的'input.nextLine',输出将是'[blank]:PlayerNumber' – Crypto

+1

@Crypto检查更新的代码。另外,你是否在for循环之前调用'nextInt'? –

+0

对不起,我对Java很陌生。我仍然获得与您的代码相同的输出。 输出: '玩家1,请输入您的姓名:请输入0到9之间的数字:' – Crypto