做,而Loop for Craps Game程序没有正确终止

时间:2013-10-05 19:45:21

标签: java loops logic do-while

我已经阅读了几个提交过的“Craps Game”标题的问题,将它与我编写的内容进行比较,似乎无法在循环中找到逻辑错误。关于该程序的一切运行良好。但是当我进入点状态时,当达到值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你进入了Point State!资金总额为:110
你需要再掷4个赢25美元 滚动任何东西但是7或你的积分你赢得10美元并再次滚动。
你滚了10你赢了10美元再次滚动!资金总额为:120
你掷了8个你赢了10美元再次滚动!资金总额为:130
你滚了4你赢了10美元再次滚动!资金总额为:140
你滚动了你的点数!你赢了25美元,但游戏结束了。资金总额为:150
你滚了7你赢了10美元再次滚动!

2 个答案:

答案 0 :(得分:0)

你需要“&&”而不是“||”。骰子必须不等于点OR不等于7.你想继续它不等于点并且不等于7.

答案 1 :(得分:0)

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

while(die1.getValue() != pointNumber && die1.getValue() != 7);
相关问题