如何使我的nextLine()等待用户的输入?

时间:2017-05-07 18:15:27

标签: java java.util.scanner

    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 :

如果我将input.nextLine更改为input.next,则会等待用户首次输入,但他/她将无法写出其全名。

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

1 个答案:

答案 0 :(得分:1)

input.nextInt()

之后使用 newLine
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);
}