简体   繁体   中英

Wrong calculation result

I have written this short code and I want to let users to enter bank balance. Then in new confirm dialog box they will choose if they want to enter transaction amount. If "YES" then they enter either positive or negative numbers. If entered value is negative so program with subtracts transaction amount from bank balance. If entered value is positive so program will add transaction value to bank balance. At the end if user selects "No" button in confirm dialog box so program will terminates with results of calculation: Question: when I enter numbers for bank balance and transactions so I get wrong answer!

I tried to user while loop and do while but I still get wrong results!

    double total = 0;
    String blc = JOptionPane.showInputDialog(null,"Enter the balance");
    double balance = Double.parseDouble(blc);

    int trcsn = JOptionPane.showConfirmDialog(null,"Transaction: ","",JOptionPane.YES_NO_OPTION);

    while(trcsn == JOptionPane.YES_OPTION){
        String transaction = JOptionPane.showInputDialog(null,"Enter amount:");
        double trc = Double.parseDouble(transaction);
        trcsn = JOptionPane.showConfirmDialog(null,"Transaction: ","",JOptionPane.YES_NO_OPTION);
        if(trc < 0){
            total = balance - trc;
        }else{
            total = balance + trc;
        }
    }
    JOptionPane.showMessageDialog(null,total);

1: I enter 1000 dollars as bank balance.

2: I enter 1050 (positive) as transaction amount.

3: I enter -500 (negative value) as transaction amount for second try.

4: Answer is 1500.00 which is wrong!

1000 + 1050 = 2050.00

2050 - 500 = 1550.00

Answer should be 1550

Why answer is wrong???

In this section:

    if(trc < 0){
        total = balance - trc;
    }else{
        total = balance + trc;
    }

You are updating your total, but not the balance. From the snippet you made, that remains unchanged.

As pointed out by @Fildor down in the comments below, at the moment you have a bug since you are either adding positive numbers together or else, subracting a negative number (x - (-y) == x + y)). To fix this, simply replace the entire if block with total = balance + trc.

You would need to update your balance to have the same value of total, or else, do without total altogether and use the balance field.

You should do something like the following:

String blc = JOptionPane.showInputDialog(null,"Enter the balance");
double balance = Double.parseDouble(blc);

int trcsn = JOptionPane.showConfirmDialog(null,"Transaction: ","",JOptionPane.YES_NO_OPTION);

while(trcsn == JOptionPane.YES_OPTION){
    String transaction = JOptionPane.showInputDialog(null,"Enter amount:");
    double trc = Double.parseDouble(transaction);
    trcsn = JOptionPane.showConfirmDialog(null,"Transaction: ","",JOptionPane.YES_NO_OPTION);
    balance += trc;
}
JOptionPane.showMessageDialog(null,balance);

First, you need to replace your if statement with a += statement. This is because subtracting when trc is negative and adding when trc is positive is equivalent to adding the absolute value of trc every time, which is probably not what you want to do. Second, you need to use 1 variable for balance, and track the changes over time. total is meaningless in the previous code, as it overrides its own value every time that your if statement executes.

    double updatedBalance = (trc < 0) ? balance - trc : balance + trc;
    total = updatedBalance;

Like the above answer suggests you need to update the balance.

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