尝试让基本骰子程序工作时出错
本文关键字:子程序 工作 出错 | 更新日期: 2023-09-27 18:36:32
我确定这是微不足道的,但我无法弄清楚。我的老师刚刚向我们展示了如何使用.下一页();昨晚,我被告知要写一个作业,刺激两个掷骰子并进行比较。我写出了代码,一切正常,直到我将dice2添加到我的console.writeline。我在这里错过了什么?
添加 dice2后,我收到以下错误,当我删除 dice2 时它们都消失了。
- ) 预期
- ;预期
- 无效的表达式术语")"
- 只有赋值、调用、递增、递减、等待和新对象表达式可以用作语句。
这是我的代码
Random m = new Random();
Random m1 = new Random();
string again;
do
{
int dice1 = m.Next(1, 6);
int dice2 = m1.Next(1, 6);
if (dice1 == 6 && dice2 == 6)
{
Console.WriteLine("You rolled boxcars");
}
else if (dice1 == 1 && dice2 == 1)
{
Console.WriteLine("You rolled snake-eyes");
}
else
{
Console.WriteLine("You rolled a {0} and a {1}", dice1 dice2);
}
Console.WriteLine("Again? (y/n)");
again = Console.ReadLine();
} while (again == "y");
编辑
添加逗号后,我注意到我得到了相同的值,所以在做了一些研究后,我删除了随机 m1 = new Random(); 并更改了 int dice2 = m1。下一页(1, 6);to int dice2 = m.Next(1, 6);它奏效了。然后我意识到它没有给我任何 6 个值,所以我将其更改为 (1, 7) 并且它工作得很好
您缺少逗号:
Console.WriteLine("You rolled a {0} and a {1}", dice1, dice2);
// ^^^
此行的 dice1
和 dice2
之间缺少逗号:
Console.WriteLine("You rolled a {0} and a {1}", dice1 dice2);
应该是:
Console.WriteLine("You rolled a {0} and a {1}", dice1, dice2);