我已經創建了一個遊戲,給玩家5次玩的機會,之後我想問玩家他們是否想再玩或退出。我已經看到它在Python中完成,但我不知道python。我的代碼工作得很好,但我想添加這兩個附加功能 如何在C#中實現這些功能? 供參考,這是我的代碼主類代碼看起來像。如何讓用戶在C#中再次嘗試或退出控制檯程序?
namespace NumBaseBall
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("\t\t\t*************************************");
Console.WriteLine("\t\t\t* Let's Have Some Fun *");
Console.WriteLine("\t\t\t* Welcome To The *");
Console.WriteLine("\t\t\t* Number Baseball Game *");
Console.WriteLine("\t\t\t*************************************\n");
GameResults gameresults = new GameResults();
for (int trysCounter = 1; trysCounter <= 5; trysCounter++)
{
gameresults.Strikes = 0;
Random r = new Random();
var myRange = Enumerable.Range(1, 9);
var computerNumbers = myRange.OrderBy(i => r.Next()).Take(3).ToList();
Console.WriteLine("The Game's Three Random Integers Are: (Hidden from user)");
foreach (int integer in computerNumbers)
{
Console.WriteLine("{0}", integer);
}
List<int> playerNumbers = new List<int>();
Console.WriteLine("Please Enter Three Unique Single Digit Integers and Press ENTER after each:");
for (int i = 0; i < 3; i++)
{
Console.Write("");
int number = Convert.ToInt32(Console.ReadLine());
playerNumbers.Add(number);
}
gameresults.StrikesOrBalls(computerNumbers,playerNumbers);
Console.WriteLine("---> Computer's Numbers = {0}{1}{2}", computerNumbers[0], computerNumbers[1], computerNumbers[2]);
Console.WriteLine("---> Player's Numbers = {0}{1}{2}", playerNumbers[0], playerNumbers[1], playerNumbers[2]);
Console.WriteLine("---> Game Results = {0} STRIKES & {1} BALLS\n", gameresults.Strikes, gameresults.Balls);
Console.WriteLine("You have played this games {0} times\n", trysCounter);
gameresults.TotalStrikes = gameresults.TotalStrikes + gameresults.Strikes;
Console.WriteLine("STRIKES = {0} ", gameresults.TotalStrikes);
if (gameresults.TotalStrikes >= 3)
{
gameresults.Wins++;
Console.WriteLine("YOU ARE A WINNER!!!");
break;
}
}
if (gameresults.TotalStrikes <3)
Console.WriteLine("YOU LOSE :(PLEASE TRY AGAIN!");
}
}
}
請解釋你的代碼不能正常工作,所以我們不必猜測。 –
請花時間格式化您的代碼,因此它不會完全放在右邊 - 並且理想地將其減少到最小*示例。這並不清楚「陣列」和「列表」標籤與該問題有什麼關係。 –
暗示解決這個問題:分開關注點。將大部分代碼從主要方法中移出到名爲「PlayGame」的方法中。此時,我們不需要關心那裏的內容 - 我們只需要確定詢問用戶是否想要再次玩,並且如果他們再次調用該方法。 –