为什么我的代码打印错误总数?

时间:2016-03-04 01:28:35

标签: java

为什么当您赔钱时我的代码会替换总金额的损失金额?我不确定错误是在其中一个函数中还是在主函数中。

替换不良的示例输出:

You have 100 dollars.
Enter an amount to bet (0 to quit):56
High, low or sevens (H/L/S):l
Die 1 rolls: 4
Die 2 rolls: 5
Total of two dice is: 9
You lost!
You have 56 dollars.
Enter an amount to bet (0 to quit):

这是我的代码:

import java.util.Scanner;
public class Project07 {


    public static void main(String[] args) {
        // Fill in the body
        int totalAmount = 100;
        while(totalAmount>0){
        boolean b = true;
        boolean c = true;
        int dollars = 100;
        Scanner inScanner = new Scanner(System.in);
        System.out.println("You have "+totalAmount+" dollars.");
        while (b){
        dollars = getBet(inScanner, dollars);
        if(dollars>0 && dollars<100){
            b=false;
        }
        }
        char sevensHL = getHighLow(inScanner);
        while(sevensHL == 'q'){
            System.out.println("ERROR! Enter H,L, or S.");
            sevensHL = getHighLow(inScanner);
        }
        int die1 = getRoll();
        int die2 = getRoll();
        System.out.println("Die 1 rolls: " + die1);
        System.out.println("Die 2 rolls: " + die2);
        int total = die1 + die2;
        System.out.println("Total of two dice is: " + total);
        int winnings = determineWinnings(sevensHL, dollars, total);
        totalAmount = totalAmount + winnings; 
        }
        System.out.println("You have 0 dollars left");
        System.out.print("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) {
        // Fill in the body
        int wager;

        System.out.print("Enter an amount to bet (0 to quit):");
        wager = inScanner.nextInt();
        if(wager == 0){
            System.out.println("You have "+ currentPool +" left");
            System.out.print("Goodbye!");
            System.exit(0);
            return 0; 
        }
        else if(wager<0 || wager>100){
            System.out.println("Your bet MUST be between 0 and 100 dollars");
            wager = 0;
            int currentDollars=currentPool - wager;
            return currentDollars;
        }
        else{
            int currentDollars=currentPool - wager;
            return currentDollars;
        }

    }

    // 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) {
        // Fill in the body

        System.out.print("High, low or sevens (H/L/S):");
        String input = inScanner.next();

        if(input.equalsIgnoreCase("h")){
            char out = 'h';
            return out;
        }
        else if(input.equalsIgnoreCase("l")){
            char out = 'l';
            return out;
        }
        else if(input.equalsIgnoreCase("s")){
            char out = 's';
            return out;
        }
        else{
            return 'q';
        }


    }

    // Produce a random roll of a single six-sided die and return that value to the calling
    // program
    private static int getRoll() {
        // Fill in the body

         int die = 1 + (int)(Math.random() * ((6 - 1) + 1));
         return die;
    }

    // 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) {
        // Fill in the body
        if (highLow == 'h' && roll >=8){
            int payout = bet;
            System.out.println("You won " + payout +" dollars!");
            return payout;
        }
        else if(highLow == 'l' && roll <= 6){
            int payout = bet;
            System.out.println("You won " + payout +" dollars!");
            return payout;
        }
        else if(highLow == 's' && roll == 7){
            int payout = 4*bet;
            System.out.println("You won " + payout +" dollars!");
            return payout;
        }
        else{
            System.out.println("You lost!");
            int payout = (-1)*bet;
            return payout;
        }
    }

}

1 个答案:

答案 0 :(得分:0)

我们似乎在getBet()方法本身中从剩余金额中扣除了赌注。这与加入/失败的结果相结合导致数量不正确。

我们需要更改getBet()方法,如下所示:

private static int getBet(Scanner inScanner, int currentPool) {
    // Fill in the body
    int wager;

    System.out.print("Enter an amount to bet (0 to quit):");
    wager = inScanner.nextInt();
    if(wager == 0){
        System.out.println("You have "+ currentPool +" left");
        System.out.print("Goodbye!");
        System.exit(0);
        return 0; 
    }
    else if(wager<0 || wager>100){
        System.out.println("Your bet MUST be between 0 and 100 dollars");
        wager = 0;
        return wager;
    }
    else{
        return wager;
    }

}

此外,我们需要传递dollars,而不是在getBet()调用中传递totalDollars,因为这是正确的金额。所以,改变:

dollars = getBet(inScanner, dollars);

dollars = getBet(inScanner, totalAmount);

进行这些更改后,应显示正确的总数。