我目前正在学校随机数猜测游戏。这只是我第二周的节目,所以这项任务对我来说至少是一个挑战。我想我已经把所有东西都放下了,除了我的循环必须在某个地方被打破(我找不到它!),因为它从来没有识别出正确的答案 - 只是指出每个猜测都是错误的。我做了什么??我希望你能帮助我:)非常感谢!麻烦猜测数字游戏在C#
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);
}
}
}
}
好的,你写了一些代码。现在,该调试它了。设置断点,检查变量并观察代码逐行执行。 – Plutonix
我想它一定是错误的。 –