你好我做了一个骰子游戏。除了游戏的赢/输方法之外,一切都很好。我不确定我做错了什么但是每当我掷骰子时它就会继续说“再次滚动”即使骰子的总和不是0.
private void RollDice(out int die1, out int die2)
{
die1 = 0;
die2 = 0;
Random rand = new Random();
die1 = 1 + rand.Next(6);
die2 = 1 + rand.Next(6);
return;
}
private int UpdateGameStatus(int sumDice)
{
int die1 = 0, die2 = 0;
int sum = die1 + die2;
if (sumDice == 8)
lblSum.Text = ("You lose!");
if (sumDice == 4)
lblSum.Text = ("You lose!");
if (sumDice == 11)
lblSum.Text = ("You lose!");
if (sumDice == 9)
lblSum.Text = ("You win!");
if (sumDice == 13)
lblSum.Text = ("You win!");
if (sumDice == 0)
lblSum.Text = ("Roll again!");
return sum;
}
private void btnRoll_Click(object sender, EventArgs e)
{
int die1 = 0, int die2 = 0
RollDice(out die1, out die2);
UpdateGameStatus(sumDice);
答案 0 :(得分:1)
在致电sumDice
之前,您使用0
分配RollDice
值。在int sumDice = die1 + die2
函数后移动RollDice
:
private void btnRoll_Click(object sender, EventArgs e)
{
int die1 = 0;
int die2 = 0;
RollDice(out die1, out die2);
int sumDice = die1 + die2;
UpdateLabel(die1, die2);
UpdatePictureBoxes(die1, die2);
UpdateGameStatus(sumDice);
}
答案 1 :(得分:0)
int die1 = 0;
int die2 = 0;
RollDice(out die1, out die2);
int sumDice = die1 + die2; //needs to be moved one down
UpdateLabel(die1, die2);
UpdatePictureBoxes(die1, die2);
UpdateGameStatus(sumDice);
答案 2 :(得分:0)
这样做。它应该工作。尝试阅读您的代码并确保在发布之前没有任何逻辑问题。这只是一个简单的逻辑错误。
private void btnRoll_Click(object sender, EventArgs e)
{
int die1 = 0;
int die2 = 0;
RollDice(out die1, out die2);
UpdateLabel(die1, die2);
UpdatePictureBoxes(die1, die2);
int sumDice = die1 + die2;
UpdateGameStatus(sumDice);
}