掷骰子骰子游戏陷入无限循环

时间:2019-11-05 05:02:41

标签: c#

我正在尝试为掷骰子游戏制作程序,用户输入下注金额,如果掷出7或11获胜,则掷出2个六个侧面骰子。如果掷出2,3或12则输掉骰子,如果掷出其他任何数字,则将数字显示为一个点。它将一直掷骰子直到掷出7或该点,如果掷出7则输掉,否则获胜。由于某种原因,掷骰子时掷骰子的次数要多于掷骰子的次数,以查看掷骰子是赢还是输。我不确定如何解决此问题 任何帮助都会被

 static int RollDice()
    {

        int roll;


        Random random = new Random();
        int die1 = 0;
        int die2 = 0;

        die1 = random.Next(1,6);

        die2 = random.Next(1,6);

        roll = die1 + die2;
        Console.WriteLine($"You rolled {die1} + {die2} = {roll}");
        return roll;

    }

    static void processCraps()
    {

        string gameStatus;
        double betAmount;
        double netWinning = 0;
        int point;


            do
            { 
                Console.WriteLine("Enter the amount to bet");
                betAmount = double.Parse(Console.ReadLine());



                if (RollDice() == 2 || RollDice() == 3 || RollDice() == 12)
                {
                    Console.WriteLine($"You lost {betAmount}");
                    netWinning = netWinning - betAmount;

                }
                else if (RollDice() == 7 || RollDice() == 11)
                {
                    Console.WriteLine($"You won {betAmount}");
                    netWinning = netWinning + betAmount;
                }
                else if (RollDice() != 2 || RollDice() != 3 || RollDice() != 12 || RollDice() != 7 || RollDice() != 11)
                {

                    point = RollDice();
                    Console.WriteLine($"Point is {point}");
                   for(int rollCount =0; rollCount <= point; RollDice() )
                {
                   if(RollDice() == 7)
                    {
                        Console.WriteLine($"You lost {betAmount}");
                        netWinning = netWinning - betAmount;
                    }
                   else if(RollDice() == point)
                    {
                        Console.WriteLine($"You won {betAmount}");
                        netWinning = netWinning + betAmount;
                    }

                }

                }

                Console.WriteLine("Do you want to play again (y/n)");
                gameStatus = Console.ReadLine();



            } while (gameStatus == "y") ;
            Console.WriteLine($"Your net winning is {netWinning}");

        }

2 个答案:

答案 0 :(得分:1)

问题是您的if语句。例如

if (RollDice() == 2 || RollDice() == 3 || RollDice() == 12)

这些RollDice()中的每一个都是对该函数的调用。如果只想滚动一次,请调用一次函数,然后将结果分配给变量并进行检查,从而:

int roll = RollDice();
if (roll == 2 || roll == 3 || roll == 12)

答案 1 :(得分:1)

您多次致电RollDice。当他们输入下注金额并将结果存储在变量中后,立即调用一次。然后使用该变量检查已滚动的值,例如

static void processCraps()
{
    string gameStatus;
    double betAmount;
    double netWinning = 0;
    int point;


    do
    { 
        Console.WriteLine("Enter the amount to bet");
        betAmount = double.Parse(Console.ReadLine());


        var diceRoll = RollDice();
        if (diceRoll == 2 || diceRoll == 3 || diceRoll == 12)
        {
            Console.WriteLine($"You lost {betAmount}");
            netWinning = netWinning - betAmount;
        }
        ... // repeat for the other rolls
    }

}