Austin
Austin

Reputation: 11

While loop for change making

I am learning Java and am making code that converts pennies in to change. It is completed however, I am unsure what to enter for the while loop. I would have used while(!change.equals("END")) but this can't be done because change is an integer, so what can I do?

class Main {

    public static void main(String args[]) {

        System.out.print("#Please enter the amount of change : ");
        int change = BIO.getInt();

        if (change <= 500 && change >= 1) {
            System.out.print("Amount" + "\t" + "Coins" + "\n");
        }

        while (change =) {
            int twopounds, pounds, fifty, twenty, ten, five, two, one;

            twopounds = change / 200;
            int left = change % 200;

            pounds = left / 100;
            left = left % 100;

            fifty = left / 50;
            left = left % 50;

            twenty = left / 20;
            left = left % 20;

            ten = left / 10;
            left = left % 10;

            five = left / 5;
            left = left % 5;

            two = left / 2;
            left = left % 2;

            one = left / 1;

            int nbCoins = twopounds + pounds + fifty + twenty + ten + five + two + one;

            if (change > 500 || change < 1) {
                System.out.print("Invalid amount " + change + "p" + "\n");
            }

            if (change <= 500 && change >= 1) {

                if (nbCoins == 1) {
                    System.out.print(change + "p " + "\t" + nbCoins + " coin ");
                } else {
                    System.out.print(change + "p " + "\t" + nbCoins + " coins ");
                }

                if (twopounds > 0) {
                    System.out.print(twopounds > 1 ? twopounds + "*200p " : "200p ");
                }

                if (pounds > 0) {
                    System.out.print(pounds > 1 ? pounds + "*100p " : "100p ");
                }

                if (fifty > 0) {
                    System.out.print(fifty > 1 ? fifty + "*50p " : "50p ");
                }

                if (twenty > 0) {
                    System.out.print(twenty > 1 ? twenty + "*20p " : "20p ");
                }

                if (ten > 0) {
                    System.out.print(ten > 1 ? ten + "*10p " : "10p ");
                }

                if (five > 0) {
                    System.out.print(five > 1 ? five + "*5p " : "5p ");
                }

                if (two > 0) {
                    System.out.print(two > 1 ? two + "*2p " : "2p ");
                }

                if (one > 0) {
                    System.out.print(one > 1 ? one + "*1p " : "1p ");
                }

                System.out.print("\n");
            }

            System.out.print("#Please enter the amount of change : ");
            change = BIO.getInt();
        }
    }
}

Thanks :)

Upvotes: 0

Views: 50

Answers (2)

Sarthak Mittal
Sarthak Mittal

Reputation: 6104

maybe this will work:

boolean flag = true;

while(flag){
    //do things 
    if(condition_when_you_want_your_loop_to_stop){
        flag = false;
    }
}

Upvotes: 0

Josh Edwards
Josh Edwards

Reputation: 908

It depends on what BIO is. Generally, there are options like .hasNextToken() if it is Enumerable. But, without knowing what that variable is declared as, I can't tell you what your trigger would be.

Upvotes: 1

Related Questions