C# - 我如何猜测随机数然后使用它?

时间:2017-07-17 15:01:29

标签: random

如何随机使用int来使用它? 我得到的错误: CS0029无法将类型'System.Random'隐式转换为'int'

        Console.Write("How old am i? Guess!");
        string randomguess = Console.ReadLine();
        Console.WriteLine("My age is " + (random.Next(1, 101)));
        int newrandomguess = Convert.ToInt32(randomguess);

        if(newrandomguess == random)
        {
            Console.WriteLine("Oh you guessed my age correctly! Im surprised.");
        }
        else
        {
            Console.WriteLine("As i expected, u guessed wrong..");
        }

1 个答案:

答案 0 :(得分:0)

创建一个新的随机值:

int randomValue = random.Next(1, 101);

使用该随机值写出控制台:

Console.WriteLine("My age is " + randomValue);

将猜测与值进行比较:

if (randomGuess == randomValue)
{
...
}