簡體   English   中英

退出游戲后擲骰子游戲循環不止一次

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

我正在嘗試制作一個程序來玩擲骰子游戲,用戶輸入下注金額,然后他們擲出 2 個六面骰子。 如果骰子的總和為 2,3 或 12,他們就輸了。 7或11他們贏了。 如果滾動任何其他數字,玩家將繼續滾動,直到他們獲得點數獲勝或 7 輸。 但是由於某種原因,如果我 select n 不再玩,它仍然會在退出前第二次循環游戲。 我不確定為什么會有任何幫助。

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

你讀了兩次輸入。

您可能希望將邏輯拆分為兩個循環。 1. 讀取投注金額。 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