while循环忽略或声明

时间:2013-12-04 01:47:52

标签: java loops while-loop

我正在尝试运行下面的while循环,为我的Programming I class提供额外的功劳。它应该模拟一个掷骰子的游戏但是我无法弄清楚为什么底部while循环运行无论如何。

import java.util.Random;
import java.util.Scanner;

public class ExtraCredit {

    public static void main(String[] args)
    {
        Scanner scan = new Scanner( System.in );
        Random random = new Random( );
        int die1, die2, roll;
        int dice = 0;
        int point = 0;
        int finish = 0;
        int play = 1;

        while (play == 1)
        {

            System.out.println ("Enter 0 to roll the dice");
            roll = scan.nextInt( );

            if (roll == 0)
            {
                die1 = random.nextInt( 6 ) + 1;
                die2 = random.nextInt( 6 ) + 1;
                dice = die1+die2;
            }

            if (dice == 7 || dice == 11)
            {
                System.out.println ("\nYou rolled a " + dice + " on your first roll!");
                System.out.println ("You win!");
                finish = 1;
            }

            else if (dice == 2 || dice == 3 || dice == 12)
            {
                System.out.println ("You rolled a " + dice + " on your first roll!");
                System.out.println ("You lose!");
                finish = 1;
            }

            else
            {
                point = dice;
                System.out.println ("\nYour point is " + point);
                System.out.println ("You need to roll a " + point + " to win");
                System.out.println ("Enter 0 to Roll again");
                roll = scan.nextInt( );
                if (roll == 0)
                {
                    die1 = random.nextInt( 6 ) + 1;
                    die2 = random.nextInt( 6 ) + 1;
                    dice = die1+die2;
                }
            }

            while (dice != point || dice != 7 || finish != 1)
            {
                System.out.println ("\nYou rolled a " + dice);
                System.out.println ("Roll again");
                roll = scan.nextInt( );
                if (roll == 0)
                {
                    die1 = random.nextInt( 6 ) + 1;
                    die2 = random.nextInt( 6 ) + 1;
                    dice = die1+die2;
                 }
            }

            if (dice == point)
            {
                System.out.println ("\nYou rolled your point (" + point +")");
                System.out.println ("You won!");
            }

            if (dice == 7)
            {
                System.out.println ("\nYou rolled a 7 before you could match your point");
                System.out.println ("You Lose!");
            }

            System.out.println ("\nEnter 1 to play again");
            play = scan.nextInt( );
        }
    }
}

2 个答案:

答案 0 :(得分:1)

(dice != point || dice != 7 || finish != 1)

这永远不会是假的。 试试这个

(dice != point && dice != 7 && finish != 1)

答案 1 :(得分:1)

您想要将||更改为&&'s

while (dice != point && dice != 7 && finish != 1)