0

我已经阅读了几个带有“掷骰子游戏”标题的提交问题,将其与我所写的内容进行比较,似乎无法在我的循环中找到逻辑错误。该程序的一切运行良好。但是当我进入点状态时,循环不会在值 7 或达到点数时终止。我为此编写了自己的 die 和 pairOfDie 类。这是代码:

        //if/else statement for entering point state on initial roll
        else
        {
            System.out.println("You rolled an " + die1.getValue());
            System.out.println("You've entered Point State!");

            //tally money total and save point number
            moneyTotal = start + point;
            pointNumber = die1.getValue();

            //print information for user
            System.out.println("Money Total is: " + moneyTotal);
            System.out.println("You need to roll another " + pointNumber
                    + " to Win $25 ");
            System.out.println("Roll anything BUT a 7 or your Point "
                            + "you win $10 and roll again.");

            /*"point state" loop that should continue to roll die unless 
            the player'spoint number or a 7 is rolled. 
            This should also keep tally of money won during each roll*/

            do
              {
               //point state roll
               die1.Value();
               System.out.println("You rolled a " + die1.getValue());
               System.out.println("You win $10 and " +
                            "get to roll again! ");
               moneyTotal += point;
               System.out.println("Money Total is: " + moneyTotal);

               }while(die1.getValue() != pointNumber || die1.getValue() != 7);

                //statement to handle if the roll comes up as point number
                //after entering point state
                if(die1.getValue()== pointNumber)
                {
                    System.out.println("You rolled your point number! "
                            + "you win $25 but the game is over. ");
                    moneyTotal += point;
                    System.out.println("Money Total is: " + moneyTotal);
                }

                //statement to handle if roll is a 7 after entering
                //point state
                else if(die1.getValue() == 7)
                {
                  System.out.println("You rolled a 7 " 
                          + "you lose $25 and the game is over");
                  moneyTotal -= lose;
                  System.out.println("Money Total is: " + moneyTotal);
                }
        }

编辑:输出是
你投了一个 4 你已经进入点状态!总钱数为:110
您需要再掷 4 才能赢得 25 美元 掷
任何东西,但 7 或您的点数您赢 10 美元并再次掷。
你掷出 10 你赢了 10 美元,然后又开始掷骰子了!总金额为:120
您掷出 8 您赢了 10 美元,然后再次掷骰!总金额为:130
您掷出 4 您赢了 10 美元,然后再次掷骰!总金额为:140
您滚动了您的点数!你赢了 25 美元,但游戏结束了。总金额为:150
您掷出 7 您赢了 10 美元,然后再次掷骰!

4

2 回答 2

0

在您的情况下,您需要“&&”,而不是“||”。骰子必然不等于点或不等于 7。当它不等于点且不等于 7 时,您想继续。

于 2013-10-05T20:06:55.240 回答
0

它必须是 AND 语句,而不是您的 while 条件中的 OR。

while(die1.getValue() != pointNumber && die1.getValue() != 7);
于 2013-10-05T20:12:12.903 回答