Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将硬币兑换成零钱_Java - Fatal编程技术网

Java 将硬币兑换成零钱

Java 将硬币兑换成零钱,java,Java,我正在学习Java,正在编写代码,将输入的硬币数转换为输入时的变化 我需要改变的一件事是我的输出,这样,如果只有一枚硬币,它只打印20便士而不是1*20便士 如果有人注意到还有其他改进,我也将不胜感激 谢谢:) 类主{ 公共静态void main(字符串参数[]){ 系统输出打印(“#请输入更改金额:”); int change=BIO.getInt(); 而(更改>0) { 两磅,五十磅,二十磅,十磅,五磅,二磅,一磅; 两磅=零钱/200; int left=更改%200; 磅=左/100;

我正在学习Java,正在编写代码,将输入的硬币数转换为输入时的变化

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

如果有人注意到还有其他改进,我也将不胜感激

谢谢:)

类主{
公共静态void main(字符串参数[]){
系统输出打印(“#请输入更改金额:”);
int change=BIO.getInt();
而(更改>0)
{
两磅,五十磅,二十磅,十磅,五磅,二磅,一磅;
两磅=零钱/200;
int left=更改%200;
磅=左/100;
左=左%100;
五十=左/50;
左=左%50;
二十=左/20;
左=左%20;
十=左/10;
左=左%10;
五=左/5;
左=左%5;
2=左/2;
1=左/1;
int nbCoins=两磅+磅+五十+二十+十+五+二+一;
如果(更改==1)
{
系统输出打印(“1枚硬币”+“\n”);
}
如果(更改>500)
{ 
系统输出打印(“无效金额”+更改+“p”+“\n”);
}
如果(更改1)
系统输出打印(更改+“p”+nbCoins+“硬币”);
{
如果(两磅>0)
{
系统输出打印(两磅>0?两磅+“*200p”:“);
}
如果(磅>0)
{
系统输出打印(磅>0?磅+“*100p”:“);
}
如果(50>0)
{
系统输出打印(五十>0?五十+“*50p:”);
}
如果(二十个>0)
{
系统输出打印(二十>0?二十+“*20p:”);
}
如果(十个>0)
{
系统输出打印(十>0?十+“*10p:”);
}
如果(五个>0)
{
系统输出打印(五个>0?五个+“*5p:”);
}
如果(两个>0)
{
系统输出打印(两个>0?两个+“*2p:”);
}
如果(一个>0)
{
系统输出打印(一个>0?一个+“*1p”:“);
}
系统输出打印(“\n”);
}
系统输出打印(“#请输入更改金额:”);
change=BIO.getInt();
}
}
}

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

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

换言之,您将
numberOfCoins
与一进行比较,如果只是一枚硬币,则只写硬币的
值。否则写入
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 " );
            }

你能做的就是把你的硬币定义为埃姆斯币, 即:

那你就可以做了

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 );
    }
那会把你的零钱印在硬币上, 你可以更进一步,定义你的钱包,把你所有的钱放在哪里 即


但是对于这个简单的场景来说,这将是一种过度的杀伤力

使用一个!不要重新发明轮子-具体地看一下选项。你应该把这篇文章发布在网站上。StackOverflow的话题已经离题了。@DaoWen仍然有bug/新功能/功能上的更改需要解决。这将使它脱离codereview和这里的主题。这里有一堆代码,但您没有告诉我们为什么它可以解决手头的问题。我将重复上面所说的。不要重新发明轮子,使用
MessageFormat
。这太可怕了。@Makoto我认为这很简单。有什么好解释的,好吧,我就这样说。以每个人都能理解和同意的方式解释为什么你的解决方案是理想的。以前看过并阅读过代码的人可能能够理解您的方法,但这并不能很好地解释您的方法。@BoristheSpider我同意,但op已经发布了多篇关于同一问题(或作业)的帖子。他可能需要这个特殊的解决方案,尽管有多种更好的方法。我试图为他的代码提供修复,而不是重写他的作业。
            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 " );
            }
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;
     }
 }