【发布时间】:2021-10-10 20:19:22
【问题描述】:
这是我的代码:
using System;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
Random r = new Random();
int Winner = r.Next(1, 10);
bool win = false;
while (win == false)
{
Console.WriteLine("Welcome to the game!! Please guess a number between 0 and 10. It is " + Winner + " to win!");
int Guess = int.Parse(Console.ReadLine());
if (Guess == Winner)
{
Console.WriteLine("Well done! " + Guess + " is correct!!");
win = true;`enter code here`
}
else if (Guess > Winner)
{
Console.WriteLine("Guess lower!!");
}
else if (Guess < Winner)
{
Console.WriteLine("Guess higher!!");
}
}
}
}
逻辑有效,但随机整数在每次循环时都会发生变化。
你可以在这里看到这个:https://dotnetfiddle.net/vNrvho
每次循环&Console.WriteLine("Welcome to the game!! Please guess a number between 0 and 10. It is " + Winner + " to win!") 再次运行时,前一个Console.WriteLine 中的随机数也会改变。
为什么随机数一直在变化?
【问题讨论】:
-
Console.WriteLine("欢迎来到游戏!请猜一个0到10之间的数字。赢了是"+Winner+"!"); - 你显示数字,然后你猜猜??
-
@KamranMammadov 是的,只是为了测试。我已经知道这个数字是多少,但这个数字一直在变化。
-
这是一个无效的问题 - 随机数根本不会改变,如果您的代码是上面的代码,这实际上是不可能的。
-
@ErmiyaEskandary 所以它要求我输入一个数字,并告诉我答案是 8。我输入 7,故意弄错了答案。它告诉我猜得更低。循环再次循环,但这次它告诉我答案是两个。
标签: c# random .net-fiddle dotnetfiddle