将便士转换为改变

时间:2014-11-28 16:35:49

标签: java

我正在学习Java并且正在制作代码,用于在输入时将转换的数量转换为更改。

我需要改变的一件事是我的输出,如果只有一枚硬币,它只打印为20p而不是1 * 20p。

如果有人注意到可以做出的任何其他改进,我也将不胜感激。

谢谢:)

class Main { 
public static void main( String args[] ) {

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

    while(change > 0)
    {
        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;

        one = left / 1;

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

        if (change == 1)
        {
            System.out.print("1 coin" + "\n");
        }

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

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

            System.out.print(change + "p " + nbCoins +" coins ");

        {

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

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

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

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

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

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

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

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

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

}
}

2 个答案:

答案 0 :(得分:0)

你可以做的是将你的硬币定义为emums, 即:

enum Coin {
    twopounds(200), pounds(100), fifty(50), twenty(20), ten(10), five(5), two(
            2), one(1);
    int value;

    Coin(int value) {
        this.value = value;
    }
    int getCoins(int amount){
        return amount/value;
    }
    int getReminder(int amount){
        return amount%value;
    }
}

然后你可以做

int change  = 321;
    for (Coin coin : Coin.values()){
        int count = coin.getCoins(change );
        if (count!=0){
            System.out.println(coin.name()+" : " +count);
        }
        change  = coin.getReminder(change );
    }

这将打印出你的硬币变化, 你可以更进一步,并在你拿着所有钱的地方定义你的钱包 即

 class Wallet{
     int money;
     int getAllCoins(Coin coin){
         int coins = coin.getCoins(money);
         money=coin.getReminder(money);
         return coins;
     }
 }

但是对于这个简单的场景来说这将是过度的

答案 1 :(得分:-1)

您不希望1个硬币的条目显示为1 *值,因此请替换比较

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

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

换句话说,你将numberOfCoins与一个比较,如果它只是一个硬币,那么只写硬币的value。否则写numberOfCoins * value

用以下代码替换代码的相关部分:

            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 " );
            }
相关问题