简体   繁体   English

退出游戏后掷骰子游戏循环不止一次

[英]Game of craps looping more than once after quitting the game

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.我正在尝试制作一个程序来玩掷骰子游戏,用户输入下注金额,然后他们掷出 2 个六面骰子。 If the sum of the dice s 2,3 or 12 they lose.如果骰子的总和为 2,3 或 12,他们就输了。 7 or 11 they win. 7或11他们赢了。 if any other number is rolled the player keeps rolling until they get the point number to win or 7 to lose.如果滚动任何其他数字,玩家将继续滚动,直到他们获得点数获胜或 7 输。 However for some reason if I select n to not play again it still loops the game a second time before quitting.但是由于某种原因,如果我 select n 不再玩,它仍然会在退出前第二次循环游戏。 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. 1. 读取投注金额。 2. Play game. 2. 玩游戏。

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");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM