在这里构建一个简单的应用程序;有问题的方法:
静态硬币类
public static void SetUpCoins() {
coin1 = new Coin();
coin2 = new Coin();
}
public static void PlayConsole() {
SetUpCoins();
OutputWinner();
}
public static void OutputWinner() {
if (coin1.ToString() == "Heads" && coin2.ToString() == "Heads") {
Console.WriteLine("You threw Heads - you win");
++point_player;
} else if (coin1.ToString() == "Tails" && coin2.ToString() == "Tails") {
Console.WriteLine("You threw Tails - I win");
++point_comp;
} else {
Console.WriteLine("You threw Odds");
WaitForKey_ConsoleOnly("Press any key to throw again");
PlayConsole();
}
Console.WriteLine("You have {0} points and the computer has {1} points", point_player, point_comp);
if (WantToPlayAgain_ConsoleOnly()) { // ask user if they want to play again; return bool
PlayConsole();
}
}
private static bool WantToPlayAgain_ConsoleOnly() {
string input;
bool validInput = false;
do {
Console.Write("Play Again? (Y or N): ");
input = Console.ReadLine().ToUpper();
validInput = input == "Y" || input == "N";
} while (!validInput);
return input == ("Y");
}
如果false
是从WantToPlayAgain_ConsoleOnly()
程序返回不退出。这是输出示例,它解释了我的问题:
为什么,当WantToPlayAgain_ConsoleOnly
为假时,程序不通过控制playConsole
方法然后退出。而不是这种重复。
运行完成OutputWinner
后,它会跳转到PlayConsole
,然后返回到OutputWinner
- 不知道为什么的 else 语句。