我正在写一个纸牌游戏。我遇到了一个问题,似乎无法弄清楚。我试图通过首先让用户指定数组大小来将玩家添加到数组中。然后要求用户输入要添加到数组中的玩家名称。我的循环有问题,在第一个循环期间,“输入名称:”将在用户输入之前打印 2 次。有人可以帮我解决这个问题。谢谢你。
public class Dealer {
Scanner keyboard = new Scanner(System.in);
private Deck deck = new Deck();
Player[] players;
public static void main(String []args){
new Dealer();
}
public Dealer(){
addPlayers();
print();
}
private void addPlayers(){
int num = numPlay();
players = new Player[num];
for(int i=0; i<num; i++){
System.out.println("Enter name: ");
String name = keyboard.nextLine();
players[i] = new Player(name);
}
}
private int numPlay(){
System.out.print("Enter how many players: ");
return keyboard.nextInt();
}
private void print(){
for(Player x: players)
System.out.println(x.toString());
}
}