我必须创建一个高低游戏。我不明白为什么它不会工作

时间:2015-10-12 03:29:04

标签: java

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;
}

我需要程序来说再见!#34;再见!&#34;当用户输入数字0时,我无法确定将其放在何处或如何使其工作。我需要帮助的另一个问题是,如果用户输入的数字高于100或小于1,程序需要说&#34;您的赌注必须在0到100美元之间&#34;。我不知道放在哪里或如何让它们工作。

2 个答案:

答案 0 :(得分:0)

您可以像这样修改getBet功能,以达到您想要的效果。

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);
}

答案 1 :(得分:-1)

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);
    }