Building a simple app here; the methods in question:
static coin class
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");
}
If false was to return from WantToPlayAgain_ConsoleOnly() the program does not exit. Here is an example of the output, which explains my problem:

Why, when WantToPlayAgain_ConsoleOnly is false, does the program not pass control the playConsole method then exit. instead of this repetition.
After OutputWinner is finished running, it then jumps into PlayConsole, and then back into the else statement of the OutputWinner – not sure why.
As the previous answers said, you have a problem when you throw odds.
Because at that point you call the exact same method again and when that call returns, you resume right at the score display and the player is asked again to play.
Because you are calling
PlayConsolerecursively, that means for every time you throw “Odd”, you will get one more prompt that you didn’t ask for.You could restructure the method like this:
That also gets rid of the recursion.