我作为一个初学者正在学习c#,并编写了一个程序,可以向用户提供一个骰子随机数,直到获得六为止。以下是我的完整代码:
using System;
class HelloWorld {
static void Main() {
Random numberGen = new Random();
int roll = 0;
int attempts = 0;
Console.WriteLine("Press enter to roll the die");
while (roll != 6) {
Console.ReadKey();
roll = numberGen(1, 7);
Console.WriteLine("You rolled " + roll);
attempts++;
}
Console.WriteLine("It took you " + attempts + " to roll a six");
Console.ReadLine();
}
}
我做错了什么,如何调试它?
numberGen(1, 7)
应该改为numberGen.Next(1, 7)
。详见文档。另外,如果你想让用户“按回车键”,请使用Console.ReadLine()
而不是Console.ReadKey()
。后者会接受 任何 按键,而不仅仅是回车键。 - 41686d6564 stands w. Palestine