我目前正在學校隨機數猜測遊戲。這只是我第二週的節目,所以這項任務對我來說至少是一個挑戰。我想我已經把所有東西都放下了,除了我的循環必須在某個地方被打破(我找不到它!),因爲它從來沒有識別出正確的答案 - 只是指出每個猜測都是錯誤的。我做了什麼??我希望你能幫助我:)非常感謝!麻煩猜測數字遊戲在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
我想它一定是錯誤的。 –