我正在制作一个随机数猜测游戏,计算机认为是1-100之间的数字。然后它会询问你它是什么,并告诉你你是对还是错。但是,每当我调试时,由于某种原因它会说它高于或低于实际随机数。此外,它同时说出其中两个陈述。另外,我不知道怎么说这个人已经猜了多少次。这是我不成功的代码。
static void Main(string[] args)
{
Random random = new Random();
int returnValue = random.Next(1, 100);
int Guess = 0;
Console.WriteLine("I am thinking of a number between 1-100. Can you guess what it is?");
while (Guess != returnValue)
{
Guess = Convert.ToInt32(Console.Read());
while (Guess < returnValue)
{
Console.WriteLine("No, the number I am thinking of is higher than " + Guess + " . Can you guess what it is?");
Console.ReadLine();
}
while (Guess > returnValue)
{
Console.WriteLine("No, the number I am thinking of is lower than " + Guess + " . Can you guess what it is");
Console.ReadLine();
}
}
while (Guess == returnValue)
{
Console.WriteLine("Well done! The answer was " + returnValue);
Console.ReadLine();
}
}
答案 0 :(得分:5)
您正在使用不需要的迭代的批次。 while语句采用布尔条件,就像IF语句一样。
static void Main(string[] args)
{
Random random = new Random();
int returnValue = random.Next(1, 100);
int Guess = 0;
Console.WriteLine("I am thinking of a number between 1-100. Can you guess what it is?");
while (Guess != returnValue)
{
Guess = Convert.ToInt32(Console.Read());
if (Guess < returnValue)
{
Console.WriteLine("No, the number I am thinking of is higher than " + Guess + ". Can you guess what it is?");
}
else if (Guess > returnValue)
{
Console.WriteLine("No, the number I am thinking of is lower than " + Guess + ". Can you guess what it is?");
}
}
Console.WriteLine("Well done! The answer was " + returnValue);
Console.ReadLine();
}
答案 1 :(得分:1)
尝试重构逻辑,使其完全符合您的要求。
Random r = new Random();
int val = r.Next(1, 100);
int guess = 0;
bool correct = false;
Console.WriteLine("I'm thinking of a number between 1 and 100.");
while (!correct)
{
Console.Write("Guess: ");
string input = Console.ReadLine();
if (!int.TryParse(input, out guess))
{
Console.WriteLine("That's not a number.");
continue;
}
if (guess < val)
{
Console.WriteLine("No, the number I'm thinking is higher than that number.");
}
else if (guess > val)
{
Console.WriteLine("No, the number I'm thinking is lower than that number.");
}
else
{
correct = true;
Console.WriteLine("You guessed right!");
}
}
答案 2 :(得分:0)
尝试改为while
s if
。如:
if (Guess < returnValue)
{
Console.WriteLine("No, the number I am thinking of is higher than " + Guess + " . Can you guess what it is?");
}
if (Guess > returnValue)
{
Console.WriteLine("No, the number I am thinking of is lower than " + Guess + " . Can you guess what it is");
}
您也可能想要输入:
Console.WriteLine("I am thinking of a number between 1-100. Can you guess what it is?");
提示在while
循环内部,以便在每次提示之前不断询问您。
答案 3 :(得分:0)
您需要将While
循环更改为if-then-else语句
会运行其代码。 所以,在你的代码中,你运行第一个 - 基本上是永远的,因为你没有重置你的条件中的任何一个值。
如果你的while循环WERE要退出,那么你的while循环就会遇到同样的问题。 你想要这样的东西:
if ( guess > myValue ) { // do something }
else ( guess < myValue ) {//do something else}
else { // do a third thing }
答案 4 :(得分:0)
正如其他人所说,你误导了while
,其中if
确实是必要的。
static void Main(string[] args)
{
Random random = new Random();
int returnValue = random.Next(1, 100);
int Guess = 0;
int numGuesses = 0;
Console.WriteLine("I am thinking of a number between 1-100. Can you guess what it is?");
while (Guess != returnValue)
{
Guess = Convert.ToInt32(Console.Read());
string line = Console.ReadLine(); // Get string from user
if (!int.TryParse(line, out Guess)) // Try to parse the string as an integer
Console.WriteLine("Not an integer!");
else {
numGuesses++;
if (Guess < returnValue)
{
Console.WriteLine("No, the number I am thinking of is higher than " + Guess + " . Can you guess what it is?");
}
if (Guess > returnValue)
{
Console.WriteLine("No, the number I am thinking of is lower than " + Guess + " . Can you guess what it is");
}
}
}
Console.WriteLine("Well done! The answer was " + returnValue + ".\nYou took " + numGuesses + " guesses.");
}
答案 5 :(得分:0)
...哥们
int total = 1,
low = 0,
high = 0;
int ranNum1,
guess;
string guessStr;
Random ranNumGen = new Random();
ranNum1 = ranNumGen.Next(1, 10);
Console.Write("Enter your guess >> ");
guessStr = Console.ReadLine();
guess = Convert.ToInt16(guessStr);
while (guess != ranNum1 )
{
while (guess < ranNum1)
{
Console.WriteLine("Your guess is to low, try again.");
Console.Write("\nEnter your guess >> ");
guessStr = Console.ReadLine();
guess = Convert.ToInt16(guessStr);
++total;
++low;
}
while (guess > ranNum1)
{
Console.WriteLine("Your guess is to high, try again.");
Console.Write("\nEnter your guess >> ");
guessStr = Console.ReadLine();
guess = Convert.ToInt16(guessStr);
++total;
++high;
}
}
//total = low + high;
Console.WriteLine("It took you {0} guesses to correctly guess {1}", total, ranNum1);
答案 6 :(得分:0)
生成1到9之间的随机数(包括1和9)。向用户询问用户是否获得该号码,然后告诉他们他们是否猜到了太低或太高,或者说是正确的。续语:让游戏继续进行,直到用户输入“退出”跟踪用户已经采取了多少猜测,当游戏结束时,打印出来。
答案 7 :(得分:0)
你好,也许这对你没问题,但对于其他想尝试的人:
using System;
namespace Exemple
{
class Program
{
static void Main(string[] args)
{
Random random = new Random();
int returnvalue = random.Next(1, 51);
Console.WriteLine(" Guess a number between 1 to 51 ");
int response = Convert.ToInt32(Console.ReadLine());
while (response > returnvalue)
{
Console.WriteLine($"No the number is low than {response} try again !");
response = Convert.ToInt32(Console.ReadLine());
}
while (response < returnvalue)
{
Console.WriteLine($"No the number is high than {response} try again !");
response = Convert.ToInt32(Console.ReadLine());
}
while (response != returnvalue)
{
Console.WriteLine($" wrong answer {response} is not the good response try again !");
response = Convert.ToInt32(Console.ReadLine());
}
Console.WriteLine($"Good ! Its {returnvalue}");
}
}
}