我目前正在学校开展随机数字猜谜游戏。这只是我的第二周编程,所以这项任务对我来说至少可以说是挑战。我想我已经把一切都搞定了,除了我的循环必须在某个地方被破坏(我找不到它!)因为它从来没有找到正确的答案 - 只是说每个猜测都是错误的。我做了什么??我希望你能帮助我:)非常感谢!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Assn3_2
{
class Program
{
public static int SelectedNumber = 0;
public static Random ran = new Random();
public static bool GameOver = false;
public static int UserMaxValue = 0;
static void Main(string[] args)
{
int UserNumber;
SelectedNumber = ran.Next(0, UserMaxValue);
do
{
Console.WriteLine("What is the maximum number you want to guess from?");
UserMaxValue = Convert.ToInt32(Console.ReadLine());
do
{
Console.WriteLine("Please make a guess between 1 and {0}", UserMaxValue);
UserNumber = Convert.ToInt32(Console.ReadLine());
GuessNumber(UserNumber);
} while (GameOver == false);
} while (GameOver == false);
}
public static void GuessNumber(int UserNumber)
{
int playagain = 0;
if (UserNumber < SelectedNumber)
Console.WriteLine("Wrong. Please try again.");
else if (UserNumber > SelectedNumber)
Console.WriteLine("Wrong.Please try again.");
else
{
Console.WriteLine("Correct! The number is {0}. Again? (1 or 2)");
playagain = Convert.ToInt32(Console.ReadLine());;
while (playagain != 1 && playagain !=2)
{
Console.WriteLine("Invalid answer. Again? Y or N");
playagain = Convert.ToInt32(Console.ReadLine());
}
if (playagain.Equals(2))
GameOver = true;
else
SelectedNumber = ran.Next(0, UserMaxValue);
}
}
}
}
答案 0 :(得分:1)
此:
SelectedNumber = ran.Next(0, UserMaxValue);
您运行此之前,提示用户填写UserMaxValue
的值,以便您有效地进行
SelectNumber = ran.Next(0, 0);
不是非常随机,当只有一个选项,并且该选择为0
时......尤其是当您只允许1
和稍后提供的最大值之间的猜测时。