I am trying to make a program to play a game of craps where the user enters a bet amount, then they roll 2 six sided dice. If the sum of the dice s 2,3 or 12 they lose. 7 or 11 they win. if any other number is rolled the player keeps rolling until they get the point number to win or 7 to lose. However for some reason if I select n to not play again it still loops the game a second time before quitting. I am not sure why any help would be appreciated.
static void processCraps()
{
string gameStatus = null;
double betAmount =0;
double netWinning = 0;
int point;
do
{
try
{
Console.WriteLine("Enter the amount to bet");
betAmount = double.Parse(Console.ReadLine());
}
catch (Exception)
{
Console.WriteLine("Invaid input try again");
}
var diceRoll = RollDice();
if (diceRoll == 2 || diceRoll == 3 || diceRoll == 12)
{
Console.WriteLine($"You lost {betAmount}");
netWinning = netWinning - betAmount;
}
else if (diceRoll == 7 || diceRoll == 11)
{
Console.WriteLine($"You won {betAmount}");
netWinning = netWinning + betAmount;
}
else if (diceRoll != 2 || diceRoll != 3 || diceRoll != 12 || diceRoll != 7 || diceRoll != 11)
{
point = diceRoll;
Console.WriteLine($"Point is {point}");
for (int rollCount = 0; rollCount >= point; rollCount++)
{
var roll = RollDice();
if (roll == 7)
{
Console.WriteLine($"You lost {betAmount}");
netWinning = netWinning - betAmount;
}
else if (roll == point)
{
Console.WriteLine($"You won {betAmount}");
netWinning = netWinning + betAmount;
}
}
}
try
{
Console.WriteLine("Do you want to play again (y/n)");
gameStatus = Console.ReadLine();
}
catch (Exception)
{
Console.WriteLine("answer must be a letter");
}
} while (gameStatus != "n") ;
Console.WriteLine($"Your net winning is {netWinning}");
}
You read input twice.
You may want to split the logic into two loops. 1. Read bet amount. 2. Play game.
do
{
Console.WriteLine("Enter the amount to bet, or 'q' to quit:");
var betStr = Console.ReadLine();
if( betStr == "q") return;
double.TryParse(betStr, out betAmount);
} while (betAmount != 0);
do
{
//Play
Console.WriteLine("Do you want to play again (n = quit)?");
gameStatus = Console.ReadLine();
} while (gameStatus != "n");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.