掷骰子跳过if语句,不打印任何内容

时间:2019-03-04 04:49:33

标签: java

我的代码不断跳过掷骰子游戏的if语句。我认为这与条件有关

不确定是否应该通过if语句将单词放在括号中。

我需要它告诉我它是否符合高/低的猜测

    System.out.println("How many points would you like to wager?");

    Scanner keyboard = new Scanner(System.in);
    int userWager = keyboard.nextInt();

    while (userWager > totalPoints){
        System.out.println("You have wagered " + userWager + " points, but only have " + totalPoints);
        System.out.println("Please try another amount.");
        userWager = keyboard.nextInt();
    }

    System.out.println("Please input LOW if you think it is between 2 and 5 or HIGH for a number between 9 and 12.");
    String userGuess = keyboard.nextLine();
    userGuess = keyboard.nextLine();

    int roll1 = (int) (Math.random() * (6 - 1) + 1);
    int roll2 = (int) (Math.random() * (6 - 1) + 1);
    int diceRoll = roll1 + roll2;

    int pointsEarned = 0;

    if (userGuess == "HIGH") {

        if ((diceRoll >= 9) && (diceRoll <= 12)) {
            pointsEarned = userWager * 2;
            System.out.println("The dice equalled " + diceRoll);
            System.out.println("Congratulations! You win " + pointsEarned);
            totalPoints = calculatePoints(pointsEarned, totalPoints);

        } else if ((diceRoll >= 2) && (diceRoll <= 5)) {
            pointsEarned = -userWager;
            System.out.println("The dice equalled " + diceRoll);
            System.out.println("Sorry! You lost " + userWager);
            totalPoints = calculatePoints(pointsEarned, totalPoints);
        }
    } else if(userGuess=="LOW"){

        if ((diceRoll>=9) && (diceRoll<=12)){
            pointsEarned=userWager*2;
            System.out.println("The dice equalled "+diceRoll);
            System.out.println("Congratulations! You win "+pointsEarned);
            totalPoints = calculatePoints(pointsEarned, totalPoints);
        } else if((diceRoll>=2) && (diceRoll<=5)) {
            pointsEarned = -userWager;
            System.out.println("The dice equalled " + diceRoll);
            System.out.println("Sorry! You lost " + userWager);
            totalPoints = calculatePoints(pointsEarned, totalPoints);
        }
    }else{
        System.out.println("The dice equalled " + diceRoll);
        System.out.println("You did not win or lose any points.");
    }

0 个答案:

没有答案