我做了一個小遊戲,但它不工作,因爲我想。 這是一個猜數字遊戲。玩家有10次機會。但是,我的問題。這會增加playerChance的價值,直到達到10.我可以用什麼來代替while?猜猜C#中的玩家機會數量
它的輸出是:
Guess the number game! Do you wanna play? y/n
y
The game started! What is your guess?
78
Your number was too high!
Your number was too high!
Your number was too high!
Your number was too high!
Your number was too high!
Your number was too high!
Your number was too high!
Your number was too high!
Your number was too high!
代碼:
namespace GuessTheNumberGame_v1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Guess the number game! Do you wanna play? y/n");
if (Console.ReadKey(true).KeyChar == 'y')
{
Console.WriteLine("The game started! What is your guess?");
int playerGuess = Convert.ToInt32(Console.ReadLine());
Random r = new Random();
int compGuess = r.Next(1, 101);
int playerScore = 0;
int playerChance = 0;
bool play = true;
do
{
while (playerChance < 10)
{
if (playerGuess > compGuess)
{
++playerChance;
Console.WriteLine("Your number was too high!");
}
else if (playerGuess < compGuess)
{
Console.WriteLine("Your number was too small!");
++playerChance;
}
else
{
++playerScore;
Console.WriteLine("You win! \nYou have: " + playerScore + " points!");
playerChance = 0;
}
}
Console.WriteLine("Do you wanna play again? y/n");
if (Console.ReadKey(true).KeyChar == 'n')
play = false;
} while (play);
}
else
{
Console.ReadKey();
}
}
}
}
當你贏了設置'playerChance = 11;' – Hackerman
也許你想迭代,而'playerChance> 0'?此外,您應該減少用戶猜測而不是增加時的機會數量。 –
這看起來像一個偉大的遊戲,但也許這個問題更適合[堆棧溢出代碼評論](https://codereview.stackexchange.com/) – Yonlif