1

I have this code, and everything is running as i want to except the face that it skips the first time it is supposed to wait for the next input by the user. Here is the code:

import java.util.Scanner;
public class mainThread {
    public static int size;
    public static int counter;
    public static Scanner input = new Scanner(System.in);
    public static String currentIn;
    public static void main(String[] args){
        System.out.println("Please type the desired amount of players: ");
        size = input.nextInt();
        String nameArray[] = new String[size];
        for(int counter = 0; counter < size; counter++){
            System.out.println("Please enter the name of player " + (counter+1));
            currentIn = input.nextLine();
            nameArray[counter] = currentIn;
            System.out.println(nameArray[counter]);
        }
    }
}

Here is what the console says:

Please type the desired amount of players: 
3
Please enter the name of player 1

Please enter the name of player 2
Jacob
Jacob
Please enter the name of player 3

It completely skips the first loop, and i can't seem to figure out why. Thanks for your time!

4

4 回答 4

1

nextInt() reads just the "3", but not the line break part. It's basically just reading up to the first token separator, but not consuming that. So if you typed in

3 Foo

and then hit return, it would take " Foo" as the name of the first player.

It sounds like you probably want to use nextLine() when finding the number of players, too:

String playerCountText = input.readLine();
size = Integer.parseInt(playerCountText); // Or use a NumberFormat

Personally I've always found Scanner to be a bit annoying like this - it sort of promises to make everything simple, but you really need to work out everything it's going to do...

于 2013-03-24T08:42:30.530 回答
1

修改程序如下:

import java.util.Scanner;
public class mainThread {
    public static int size;
    public static int counter;
    public static Scanner input = new Scanner(System.in);
    public static String currentIn;
    public static void main(String[] args){
        System.out.println("Please type the desired amount of players: ");
        size = Integer.parseInt(input.nextLine().trim());
        String nameArray[] = new String[size];
        for(int counter = 0; counter < size; counter++){
            System.out.println("Please enter the name of player " + (counter+1));
            currentIn = input.nextLine();
            nameArray[counter] = currentIn;
            System.out.println(nameArray[counter]);
        }
    }
}

问题是\n留在缓冲区中,这就是为什么.nextLine()被跳过了..

编辑,以下程序还完成了一些输入验证:

import java.util.Scanner;
public class mainThread {
    public static int size;
    public static int counter;
    public static Scanner input = new Scanner(System.in);
    public static String currentIn;
    public static void main(String[] args){
        boolean valid = false;
        System.out.println("Please type the desired amount of players: ");
        while(valid == false){
         try{
           size = Integer.parseInt(input.nextLine().trim());
           valid = true;
         }catch(Exception e){
            System.out.println("Error input, try again");
         }
        }
        String nameArray[] = new String[size];
        for(int counter = 0; counter < size; counter++){
            System.out.println("Please enter the name of player " + (counter+1));
            currentIn = input.nextLine();
            nameArray[counter] = currentIn;
            System.out.println(nameArray[counter]);
        }
    }
}
于 2013-03-24T08:44:47.913 回答
0

我不确定问题是什么,但如果你更换线路

currentIn = input.nextLine();

和:

currentIn = input.next();

它会起作用的。

于 2013-03-24T08:46:27.937 回答
0

基本上事情是当您输入 3 然后按 enter 时,输入计为一个新行值,它留在缓冲区中,当您尝试在循环中读取新行时

currentIn = input.nextLine();

第一次将存储的新行作为输入。您可以通过添加 currentIn = input.nextLine(); 来避免这种情况 在 size = input.nextInt() 之后;

或从文件中读取输入。

于 2013-03-24T09:03:15.110 回答