非常基本,但是我正在创建一个游戏,我将在游戏中有2到4个玩家我需要知道如何向用户询问将有多少玩家然后将这个数量存储在我的数组中供以后使用?!
这是我到目前为止所写的内容
{
int NumberofPlayers;
{
do
{
Console.WriteLine("Please enter number of players (2-4): ");
String StringNumberOfPlayers = Console.ReadLine();
NumberofPlayers = int.Parse(StringNumberOfPlayers);
}
while (NumberofPlayers > 4 || NumberofPlayers < 2);
}
// need get the number of players and set the required elements in
// playerPositions to 0 on the board
}
static int [] PlayerPositions = new int [4];
static void Main()
{
ResetGame();
}
}
}
答案 0 :(得分:3)
您走在正确的轨道上,只需分配大小为NumberofPlayers
static int [] PlayerPositions;
public void ResetGame()
{
int NumberofPlayers;
do
{
Console.WriteLine("Please enter number of players (2-4): ");
String StringNumberOfPlayers = Console.ReadLine();
NumberofPlayers = int.Parse(StringNumberOfPlayers);
}
while (NumberofPlayers > 4 || NumberofPlayers < 2);
// need get the number of players and set the required elements in
// playerPositions to 0 on the board
PlayerPositions = new int [NumberofPlayers];
}
答案 1 :(得分:1)
将数组大小设置为NumberofPlayers。
PlayerPositions = new int [NumberofPlayers];
答案 2 :(得分:0)
为此,您为什么不创建一个PlayerPosition列表。
List<PlayerPositions> players = new List<PlayerPositions>();
根据用户的输入,将这些对象添加到上面的列表中。
while (NumberOfPlayers > 0)
{
players.Add(new PlayerPositions());
NumberOfPlayers--;
}