随机数猜谜游戏
本文关键字:游戏 随机数 | 更新日期: 2023-09-27 18:22:06
我正在做一个随机数字猜谜游戏,电脑会想到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();
}
}
您正在使用大量不需要的迭代。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.ReadLine());
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();
}
尝试重组逻辑,使其完全符合您的要求。
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!");
}
}
尝试将while
设置为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
循环中的提示,因此它将在每次提示之前不断询问您。
您需要将While
循环更改为if-then-else语句
只要语句为true,while就会运行其代码。因此,在您的代码中,您运行第一个——基本上是永远运行,因为您没有重置条件中的任何一个值。
如果while循环要退出,那么其他while循环也有同样的问题。你想要这样的东西:
if ( guess > myValue ) { // do something }
else ( guess < myValue ) {//do something else}
else { // do a third thing }
正如其他人所说,在真正需要if
的地方,您滥用了while
。
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.");
}
老兄。。。
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);
生成一个介于1和9(包括1和9)之间的随机数。请用户获取数字,然后告诉他们猜测的是过低还是过高,或者完全正确。附加功能:让游戏继续进行,直到用户键入"退出",记录用户进行了多少次猜测,当游戏结束时,打印出来。
你好,也许这对你来说没问题,但对其他想尝试的人来说:
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}");
}
}
}