我是c#和编码的新手。为了尝试提高我的技能,我正在尝试创建一个基本游戏,其中两个玩家掷骰子并记录他们的分数。玩家赢得20分。每个玩家轮流掷骰子将他们的第一个掷骰子加到他们的第二个等等,直到其中一个达到20.玩家如果掷出六个就可以再次掷骰子。
我目前的代码是:
do
{
Console.Write("Enter the name of Player 1: ");
Player[0] = Console.ReadLine();
Console.Write("Enter the name of Player 2: ");
Player[1] = Console.ReadLine();
Random DiceRandom = new Random();
DiceThrow[0] = DiceRandom.Next(1, 7);
int i = 0;
while (i <= 1)
{
DiceThrow[0 + i] = DiceRandom.Next(1, 7);
Console.ReadLine();
Console.Write(Player[0 + i] + " rolled a " + DiceThrow[0 + i]);
if (DiceThrow[0 + i] != 6) i++;
}
Console.ReadLine();
PlayerTotal[0] = DiceThrow[0];
PlayerTotal[1] = DiceThrow[1];
Console.ReadLine();
Console.Write(Player[0] + " currently has " + PlayerTotal[0]);
Console.ReadLine();
Console.Write(Player[1] + " currently has " + PlayerTotal[1]);
Console.ReadLine();
}
while (PlayerTotal[0] == 20);
while (PlayerTotal[1] == 20);
我特别挣扎的是将球员第一次加注到第二次滚动。如果玩家掷出6,那么它会将6加到他们在重新滚动中获得的内容。
任何帮助都将不胜感激。
答案 0 :(得分:3)
您的代码存在许多问题:
这只是一个简短的概述..
这可以是一个解决方案:
Console.Write("Enter the name of Player 1: ");
Player[0] = Console.ReadLine();
Console.Write("Enter the name of Player 2: ");
Player[1] = Console.ReadLine();
Random DiceRandom = new Random();
do
{
int i = 0;
while (i <= 1)
{
int thisThrow = DiceRandom.Next(1, 6);
DiceThrow[0 + i] += thisThrow;
Console.ReadLine();
Console.Write(Player[0 + i] + " rolled a " + thisThrow);
if (thisThrow != 6) i++;
}
Console.ReadLine();
PlayerTotal[0] += DiceThrow[0];
PlayerTotal[1] += DiceThrow[1];
Console.ReadLine();
Console.Write(Player[0] + " currently has " + PlayerTotal[0]);
Console.ReadLine();
Console.Write(Player[1] + " currently has " + PlayerTotal[1]);
Console.ReadLine();
}
while (PlayerTotal[0] < 20 && PlayerTotal[1] < 20);
答案 1 :(得分:1)
您的问题是您使用以下行重置以前的分数:
PlayerTotal[0] = DiceThrow[0];
PlayerTotal[1] = DiceThrow[1];
您应该将其更改为使用+=
,如下所示:
PlayerTotal[0] += DiceThrow[0];
PlayerTotal[1] += DiceThrow[1];
这就是说:PlayerTotal[0] = PlayerTotal[0] + DiceThrow[0];
除此之外,您的代码中还有一些问题。
例如,您在代码的开头有一个Do
但是while
...
您可能希望使用 AND 语句创建一个While
。此外,{/ 1}}语句需要 之后才能获得用户名...
例如: //获取用户名
Do
答案 2 :(得分:0)
int i = 0;
while (i <= 1)
{
int thisThrow = DiceRandom.Next(1, 6);
DiceThrow[0 + i] += thisThrow;
Console.ReadLine();
Console.Write(Player[0 + i] + " rolled a " + thisThrow);
if (thisThrow != 6) i++;
}
Console.ReadLine();
PlayerTotal[0] += DiceThrow[0];
PlayerTotal[1] += DiceThrow[1];
Console.ReadLine();
Console.Write(Player[0] + " currently has " + PlayerTotal[0]);
Console.ReadLine();
Console.Write(Player[1] + " currently has " + PlayerTotal[1]);
Console.ReadLine();