我正試圖寫一個簡單的控制檯遊戲應用程序&我遇到了一些idk時刻。好的,所以在代碼中,我試圖得到一個Y/N輸入,並且如果是Y則開始遊戲,或者如果是N則退出。我很想聽聽一些各種解決方案&對於編碼示例有獎金< 3每次開始或播放遊戲時都會指定隨機門號。如何在循環中詢問是/否答案?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Threading;
namespace Desicions
// Typo I know.
{
class Program
{
static void Main(string[] args)
{
bool WrongInput = true;
while (WrongInput)
{
Console.Clear();
Console.WriteLine("Would you perfer what's behind door 1, 2, or 3?");
string userValue = Console.ReadLine();
if (userValue == "1")
{
Console.WriteLine("You won a new car!");
WrongInput = false;
}
else if (userValue == "2")
{
Console.WriteLine("You won a boat!");
WrongInput = false;
}
else if (userValue == "3")
{
Console.WriteLine("Aww, you did not win this time please play again.");
WrongInput = false;
}
else
{
Console.WriteLine("Please choose door number 1, 2, or 3. Press Enter to return to game.");
Console.ReadLine();
}
}
Thread.Sleep(2000);
Console.WriteLine();
Console.WriteLine("Would you like to play again? (Y/N)");
while ((Console.ReadLine() == "Y") || (Console.ReadLine() == "y"))
//Should I use this instead?
//while String.Equals(Console.ReadLine(), "y", StringComparison.CurrentCultureIgnoreCase);
//if so, why?
{
//What goes here for (Y)/(y) input to return into top of while (WrongInput) loop?
Console.WriteLine("Currently in Dev [:");
}
while ((Console.ReadLine() == "N") || (Console.ReadLine() == "n"))
{
//I would like for a (N)/(n) input to WriteLine("Exiting game..."); Thread.Sleep(2000); then exit. Solutions plz por favor?
}
//BONUS c# <3 for code sample for setting doors numbers randomly!
Console.ReadLine();
}
}
}
看我如何選擇一個隨機門編輯:d – Aybe