&& 和 || in while 循环问题

&& and || in while loop problems

我被指派为我的 java 课程介绍制作高或低的骰子游戏。我已经完成了所有方法,但是,我需要使用一个 while 循环,以便我可以继续玩游戏,直到我的现金达到 0 或者我下注 0 美元。如果不需要,我不想休息。所以我的问题是我能做什么(如果可能的话)只使用 while 循环?这是我的程序:

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int cash = 100;
    int bet = 1

    while(cash > 0 || bet != 0)
    {

        bet = getBet(in, cash);

        char choice = getHighLow(in);

        int roll1 = getRoll();
        System.out.println("Dice 1 rolls: " + roll1);

        int roll2 = getRoll();
        System.out.println("Dice 2 rolls: " + roll2);

        int total = (roll1 + roll2);
        System.out.println("The total roll is: " + total);

        int winnings = determineWinnings(choice, bet, total);

        cash = cash + winnings;
    }

    System.out.println("You have " + cash + " dollars left. Goodbye!");



}


// Given a Scanner and a current maximum amount of money, prompt the user for
// an integer representing the number of dollars that they want to bet.  This
// number must be between 0 and to maximum number of dollars.  If the user enters
// a number that is out of bounds, display an error message and ask again.
// Return the bet to the calling program.
private static int getBet(Scanner inScanner, int currentPool) {
    System.out.println("You have " + currentPool + " dollars");
    System.out.println("Enter and amount to bet (0 to quit): ");

    int bet = inScanner.nextInt();


    while (0 > bet || currentPool < bet )
    {
        System.out.println("Your bet must be between 0 and " + currentPool + " dollars ");
        bet = inScanner.nextInt();
    }

    return bet;

}

// Given a Scanner, prompt the user for a single character indicating whether they
// would like to bet High ('H'), Low ('L') or Sevens ('S').  Your code should accept
// either capital or lowercase answers, but should display an error if the user attempts
// to enter anything but one of these 3 values and prompt for a valid answer.
// Return the character to the calling program.
private static char getHighLow(Scanner inScanner) {
    System.out.println("High, low or sevens (H/L/S): ");

    inScanner.nextLine();

    char choice = inScanner.nextLine().charAt(0);
    choice = Character.toUpperCase(choice);

    while (choice != 'H' && choice != 'L' && choice != 'S')
    {
        System.out.println("You must choose between high, low or sevens (H/L/S): ");
        choice = Character.toUpperCase(inScanner.nextLine().charAt(0));
    }

    return choice;


}

// Produce a random roll of a single six-sided die and return that value to the calling
// program
private static int getRoll() {
    Random generate = new Random();

    int roll = generate.nextInt(6) + 1;

    return roll;

}

// Given the choice of high, low or sevens, the player's bet and the total result of
// the roll of the dice, determine how much the player has won.  If the player loses
// the bet then winnings should be negative.  If the player wins, the winnings should
// be equal to the bet if the choice is High or Low and 4 times the bet if the choice
// was Sevens.  Return the winnings to the calling program.
private static int determineWinnings(char highLow, int bet, int roll) {

    if(roll <= 6)
    {
        if(highLow == 'L')
        {
            System.out.println("You won " + bet + " dollars! ");
            return bet;
        }

        else
            System.out.println("You lose! ");
            return -bet;
    }

    else if (roll == 7)
    {
        if(highLow == 'S')
        {
            System.out.println("You won " + (bet * 4) + " dollars! ");
            return (bet * 4);
        }

        else
            System.out.println("You lose! ");
            return -bet;
    }

    else
    {
        if(highLow == 'H')
        {
            System.out.println("You won " + bet + " dollars! ");
            return bet;
        }

        else
            System.out.println("You lose! ");
            return -bet;
    }

}

}

while(cash > 0 || bet != 0) 更改为 while(cash > 0 && bet != 0)

这是因为,如果现金等于 0 或赌注等于 0,您希望结束游戏。如果您使用 ||,那么只有当两个变量都为 0 时,循环才会停止。如果您使用 &&,那么如果其中一个变量为 0,则循环将停止。

当使用 && 时,两个 条件都必须为真才能使循环 执行 。如果 一个条件为真而另一个为假,则循环将停止

当使用||时,任何一个 对于 执行 的循环,条件必须为真。如果两个条件都为真,则循环将 运行。如果一个条件为假,另一个为真,那么它仍然会 运行。如果 both 条件都是 false 那么,循环将 stop.

编辑:

如果您希望程序在 bet 变量为 0 时立即结束,则只需在 bet = getBet(in, cash); 之后添加这些行:

if(bet<=0){
 System.out.println("You are out of cash!");
 break;
} 

希望这对您有所帮助:)