[英]How can i generate a new random number after one round in c#?
I have a problem with my programm.我的程序有问题。 I want to write a random number guessing game.我想写一个随机数猜谜游戏。 I have actually everything but when i ask the user if he/she wants to do it again and he/she says yes, the same number from the 1. round is also in the second round.我实际上拥有一切,但是当我问用户他/她是否想再做一次并且他/她说是时,第一轮中的相同数字也在第二轮中。 Like:喜欢:
You have to actually call rnd.Next() everytime you want a different random number:每次你想要一个不同的随机数时,你都必须实际调用 rnd.Next() :
Random rnd = new Random();
Int32 RandomNumber;
for(int i = 0; i < 100; i++)
{
RandomNumber = rnd.Next(1, 100);
Console.WriteLine(RandomNumber);
}
I've prepared a little fotnetfiddle for your convenience.为了您的方便,我准备了一个小调子。
You should generate new random number for each game round :您应该为每一轮游戏生成新的随机数:
public static void Main() {
// wrong place: random value is the same for all rounds
//int zufallligezahl = new Random().Next(1, 100);
...
try {
do {
// right place: each game has its own random value
// In case of .Net 6 put it as
// ... = Random.Shared.Next(1, 100);
int zufallligezahl = new Random().Next(1, 100);
do {
Console.Write("Erraten Sie die Zahl indem...");
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.