static void Main(string[] args)
{
Random randomNumbers = new Random();
int cardPlayer = randomNumbers.Next(0, 11);
int cardPC = randomNumbers.Next(0, 11);
NextCard:
Console.WriteLine("Do you want another card ? yes/no You have now " + cardPlayer);
String choice = Console.ReadLine();
if (choice == "yes") {
cardPlayer += randomNumbers.Next(0, 11);
if (cardPC < 15) {
cardPC += randomNumbers.Next(0, 11);
}
goto NextCard;
} else if (choice == "no") {
if (cardPlayer <= 21 && (cardPC > 21 || cardPC < cardPlayer)) {
Console.WriteLine("WIN ! Computer have " + cardPC + " cards!");
} else if (cardPlayer <= 21 && (cardPC > 21 || cardPC > cardPlayer)) {
Console.WriteLine("LOSE ! Computer have " + cardPC + " cards!");
} else if (cardPC > 21 && cardPlayer > 21) {
Console.WriteLine("Both players lose ! Computer have " + cardPC + " cards! ");
} else if (cardPC == cardPlayer) {
Console.WriteLine("DRAW!");
}
Console.ReadKey();
} else {
Console.WriteLine("Type only yes/no!");
goto NextCard;
}
}
答案 0 :(得分:0)
您可以使用goto
循环解决问题,而不是使用do-while
:
Random randomNumbers = new Random();
int cardPlayer = randomNumbers.Next(0, 11);
int cardPC = randomNumbers.Next(0, 11);
String choice;
do
{
Console.WriteLine("Do you want another card ? yes/no You have now " + cardPlayer);
choice = Console.ReadLine();
cardPlayer += randomNumbers.Next(0, 11);
if (cardPC < 15)
{
cardPC += randomNumbers.Next(0, 11);
}
} while (choice == "yes" && cardPlayer < 21);
我们通过在绘制新卡之前测试cardPlayer
变量是否小于21来解决您的问题。