简体   繁体   中英

I have to create a High Low game. I don't understand why it won't work

public static void main(String[] args) {
    int money = 100, roll1, roll2;
    int userBet;
    char c;
    int lostwin;
    Scanner in = new Scanner(System.in);
    do {
        if (money == 0 || money < 0)
            break;
        System.out.println(" You have " + money + "  dollars. ");
        userBet = getBet(in, money);
        if (userBet == 0)
            break;
        c = getHighLow(in);
        roll1 = getRoll();
        System.out.println(" Die 1 rolls : " + roll1);
        roll2 = getRoll();
        System.out.println(" Die 2 rolls : " + roll2);
        System.out.println("Total of two dice is: " + (roll1 + roll2));
        lostwin = determineWinnings(c, userBet, roll1 + roll2);
        if (lostwin < 0)
            System.out.println("You lost!");
        else
            System.out.println("You won " + lostwin + " dollars! ");
        money = money + lostwin;
    } while (true);
}

private static int getBet(Scanner inScanner, int moneyPot) {
    System.out.println("Enter an amount to bet (0 to quit): ");
    int result = inScanner.nextInt();
    if (result > moneyPot) {
        do {
            System.out.println("Enter an amount to bet (0 to quit): ");
            result = inScanner.nextInt();
        } while (result > moneyPot);
    }
    return result;
}

private static char getHighLow(Scanner inScanner) {
    System.out.println("High, low or sevens (H/L/S): ");
    String str = inScanner.next();
    return str.charAt(0);
}

private static int getRoll() {
    return (int) (Math.random() * 6) + 1;
}

private static int determineWinnings(char highLow, int bet, int roll) {
    int result = 0;
    if (highLow == 'H') {
        if (roll < 7) {
            result = -1 * bet;
        } else {
            result = bet;
        }
    }
    if (highLow == 'L') {
        if (roll > 7) {
            result = -1 * bet;
        } else {
            result = bet;
        }
    }
    if (highLow == 'S') {
        if (roll == 7) {
            result = 4 * bet;
        } else {
            result = -1 * bet;
        }
    }
    return result;
}

I need the program to say "Goodbye!" when the user enters the number 0, but I can't figure out where to put it or how to get it to work. The other issue I need help with is if the user enters a number higher than 100 or less than 1, the program needs to say "Your bet MUST be between 0 and 100 dollars". I don't know where to put them or how to get them to work.

You can modify your getBet function like this to achieve what you want.

private static int getBet(Scanner inScanner, int moneyPot) {

    int result;
    System.out.println("Enter an amount to bet (1-100) (0 to quit): ");
    do {
        result = inScanner.nextInt();
        if (result == 0) {
            System.out.println("Good Bye");
            return result;
        } else if (result < 0 && result > 100) {
            System.out.println("Please enter an amount between (1-100) (0 to quit)");
        } else if (result > moneyPot) {
            System.out.println("Please enter an amount less than your moneyPot between (1-100)  (0 to quit)");
        } else {
            return result;
        }
    } while (true);
}
public static void main(String[] args) {
        int money = 100;
        int roll1;
        int roll2;
        int userBet;
        int lostwin;
        char c;
        Scanner in = new Scanner(System.in);
        do {
            if (money < 1)
                break;
            System.out.println(" You have " + money + "  dollars. ");
            userBet = getBet(in, money);
            if (userBet == 0)
                break;
            c = getHighLow(in);
            roll1 = getRoll();
            System.out.println(" Die 1 rolls : " + roll1);
            roll2 = getRoll();
            System.out.println(" Die 2 rolls : " + roll2);
            System.out.println("Total of two dice is: " + (roll1 + roll2));
            lostwin = determineWinnings(c, userBet, roll1 + roll2);
            if (lostwin < 0)
                System.out.println("You lost!");
            else
                System.out.println("You won " + lostwin + " dollars! ");
            money = money + lostwin;
        } while (money > 0);

        System.out.println("Goodbye!");
    }

    private static int getBet(Scanner inScanner, int moneyPot) {
        int bet;

        do {
            System.out.println("Enter an amount to bet (0 to quit): ");
            bet = inScanner.nextInt();
        } while (bet > moneyPot && bet != 0);

        return bet;
    }

    private static char getHighLow(Scanner inScanner) {
        System.out.println("High, low or sevens (H/L/S): ");
        String str = inScanner.next();

        return str.charAt(0);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM