为什么此代码允许输入的玩家少于numPlayers?

时间:2016-05-25 21:21:55

标签: java arrays

我是Java和编码的新手。该代码用于输入用户数量的玩家的整数,然后输入总计该玩家数量的玩家的名字;但是,在运行时,它允许输入少一个名称。

import java.util.Arrays;
//import java.util.Scanner;

    public class FarkleFactory {

        public static void main(String[] args) {
            // Welcome and set-up for a Farkle game

            int numPlayers;
            String playerOrder;

            System.out.println ("Let's play Farkle!");
            //Scanner input = new Scanner (System.in);
            TextIO input = new TextIO();

            System.out.println("How many players will there be?");
            numPlayers = input.getInt();
            String[] players = new String[numPlayers];
            System.out.println("Roll the dice and determine who starts...");
            System.out.println("Enter the players.");

            for (int i = 0; i < numPlayers; i++)  { 
                players[i] = TextIO.getln();
            }

            System.out.println(Arrays.toString(players));
            System.out.println ("OK, " + players[1] + "! Roll the dice and get started!");
        }
    }

1 个答案:

答案 0 :(得分:0)

尝试替换

numPlayers = input.getInt();

numPlayers = input.getlnInt();

否则,您输入的换行符为&#34;玩家数量&#34;被读作第一名球员的名字。