简体   繁体   中英

Printing balance, What m i doing wrong?

I want to maintain 100 Balance at all cost. Please tell me what mi doing wrong?

    static void withdraw(float cash){

    if (balance>100)

    balance=balance-cash;
    System.out.println(balance);

    else 
        (balance<=100){
        System.out.println("You do not have enough balance");

    }
}

Probably should be something more like:

if ((balance - cash) < 100) {
    System.out.println("You do not have enough balance");
} else {
    balance -= cash;
    System.out.println(balance);
}

Most tutorials are going to build up more complex idea from simpler ones, so taking the time to move through these steps is important. In production code you'd want to return the balance from this function, and would want to remove any redundant code. In the code above, we have redundant code when we determine whether the withdrawal brings the balance total below 100, and when we actually reduce the balance by the withdrawal amount in the else statement.

If you'll notice we actually perform the same mathematical operation twice once here: ((balance - cash) < 100)

and again here: balance -= cash;

To remove this, we can do the calculation once and store it in a variable. This is necessary because we don't know yet whether the balance will fall below 100 after this operation. Without storing it in a variable we either need to repeat the operation as we already do, or add back in the withdrawal amount after if the balance becomes too low, which is an additional operation we don't need to do. That would look like this:

balance -= cash;
if (balance < 100) {
    System.out.println("You do not have enough balance");
    balance += cash;
} else {
    System.out.println(balance);
}

But this still requires one extra step - addition in the case our if statement fails. Instead, what we can do is assign the result of our initial subtraction to a temporary variable, and if the if condition fails we can simply throw that away without any additional operations. Like this:

float tempbalance = balance - cash;
if (tempbalance < 100) {
    System.out.println("You do not have enough balance");
} else {
    balance = tempbalance;
    System.out.println(balance);
}

This way we're not repeating ourselves or doing additional operations that we could eliminate. This first principle you'll hear repeated as DRY - do not repeat yourself. This just means any time you reuse the same code, operation, steps or procedure (here, subtraction) more than once, you should reconsider your code design and try and find a way to make that code reusable (a very good use for functions).

Ultimately I think your tutorial is going to have you return the balance rather than print it. If it doesn't in your current lesson, at some point the course you're working through will introduce this idea as it makes much cleaner code in this case. For example, refactoring this code we could write a function that looks like:

static float withdraw(float cash) {
    float tempbalance = balance - cash;
    if (tempbalance < 100) {
        return balance;
    } else {
        return tempbalance;
    }
}

We would likely create an additional function that looks like this:

static void makeWithdraw(float cash) {
    if (balance == (balance = makeWithdraw(cash))) {
        System.out.println("You do not have enough balance");
    } else {
        System.out.println(balance);
    }
}

With this design we can call makeWithdraw() any time we want to output to the user whether they have enough money to make their withdraw, or their balance after the withdraw. We're also free to reuse our withdraw() function anywhere else in our code that we need to make withdraws, but don't want to print these messages. This also solves the problem of repeating code, and is a safe way to blindly perform operations on the balance without worry that it will be modified in a way we don't expect.

Hopefully this is clearer. Note that in the makeWithdraw() function it's necessary that we both have the comparison made in this order (balance == (balance = withdraw(cash))) , as well as having the assignment statement (balance = withdraw(cash)) within separate parenthesis. If you reverse the order you'll find we're always told we don't have enough balance because we modify the value of balance before we make the comparison, resulting in a true evaluation each time. And if we don't properly encapsulate the assignment statement, Java will not be able to properly interpret what we mean.

Hopefully that helps and makes your lesson easier. Here's the code all together in a class that separates these functions by themselves so you can see it as a whole:

public class BalanceTutorial {

    static float balance = 300;

    public static void main(String[] args) {
        makeWithdraw(100);
        makeWithdraw(100);
        makeWithdraw(1);
    }

    static float withdraw(float cash) {
        float tempbalance = balance - cash;
        if (tempbalance < 100) {
            return balance;
        } else {
            return tempbalance;
        }
    }

    static void makeWithdraw(float cash) {
        if (balance == (balance = withdraw(cash))) {
            System.out.println("You do not have enough balance");
        } else {
            System.out.println(balance);
        }
    }

}

Just put like this

    int cash = 10;
    int balance = 100;
    if (balance-cash>100){  
        balance=balance-cash;
        System.out.println(balance);
    }else
        System.out.println("You do not have enough 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