我正在写一个随机机会游戏来选择一个随机的赢家。我正在使用 for 循环将玩家输入到数组中,但它不允许我为第一个玩家输入任何内容。这是代码:
import java.util.Scanner;
import java.util.Random;
public class Run {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
Random rand = new Random();
System.out.println("How many people will play???");
int playernum = input.nextInt();
String players[] = new String[playernum];
for(int i = 0; i < playernum; i++){
System.out.println("Who is player #" + (i+1)+"?");
players[i] = input.nextLine();
}
System.out.println("The winner is: " + players[rand.nextInt(playernum)]);
}
}