同时循环换制作

时间:2014-12-01 14:33:14

标签: java

我正在学习Java并且正在制作能够改变便士的代码。但是,它完成了,我不确定为while循环输入什么。我会使用while(!change.equals("END"))但是这不能完成,因为更改是一个整数,所以我该怎么办?

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

谢谢:)

2 个答案:

答案 0 :(得分:1)

这取决于BIO是什么。通常,如果它是Enumerable,则有.hasNextToken()等选项。但是,在不知道该变量被声明为什么的情况下,我无法告诉您触发器的内容。

答案 1 :(得分:0)

也许这会奏效:

boolean flag = true;

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