跳过扫描(Java)

时间:2014-07-10 15:06:38

标签: java java.util.scanner

我开始用java练习。我创建了类“Player”,其中包含:

    import java.util.Scanner;

public class Player {
    private int maxOfGuess = 100;
    private int minOfGuess = 0;
    public String Name;
    public int type;


}

我还写了一个代码:(在另一个类中)

import java.util.Scanner;

public class GuessGame {
    static int DEAFULT_MAX = 100;
    static int NUMBER_TO_GUESS = (int)(Math.random() * (DEAFULT_MAX - 0) + 1);

    public static void main(String[] args){
        guessGameProcess();
    }

    public static int guessGameProcess(){
        Scanner scan = new Scanner(System.in);
        System.out.println("Hello there! Please enter the number of players (Between 1 - 6)");

        int numberOfPlayers = scan.nextInt();
        if(numberOfPlayers < 1 || numberOfPlayers > 6){
            System.out.println("Illegal number of players!");
            scan.close();
            return (-1);
        }
        Player[] arrOfPlayers = new Player[numberOfPlayers];
        System.out.println("Now, Please enter the name of each player, and his type.");
        System.out.println("There are some types: Human, Monkey, Computer, Smart Computer");

        scan.nextLine();
        for(int i=0;i<numberOfPlayers;i++){
            arrOfPlayers[i] = new Player();
            System.out.println("Please enter the Name of the " + (i+1) + " player:");
            arrOfPlayers[i].Name = scan.nextLine();
            System.out.println("Please enter the type of the player : ");
            System.out.println("0. Human\t1. Monkey\t2. Computer\t3. Smart Computer");
            arrOfPlayers[i].type = scan.nextInt(); 
        }

        scan.close();
        return 0;
    }
}

由于某种原因,从第二个获取名称操作出现问题,它会跳过获取名称,我不知道为什么。例如,我输入了一个名字,这就是发生的事情:

Please enter the Name of the 1player:
name1
Please enter the type of the player : 
0. Human    1. Monkey   2. Computer 3. Smart Computer
2
Please enter the Name of the 2player:
Please enter the type of the player : 
0. Human    1. Monkey   2. Computer 3. Smart Computer

有人能解释我为什么吗?谢谢。

0 个答案:

没有答案