Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/347.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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_Arrays_Function - Fatal编程技术网

Java,显示硬币、数组和函数的变化?

Java,显示硬币、数组和函数的变化?,java,arrays,function,Java,Arrays,Function,我的函数ReturnChange应该接受change,coins数组int coins[]={100,50,20,10,5};p、 它们以美分为单位,numCoins数组int numCoins[]={10,10,10,10};然后计算变化。零钱应该是硬币。如果硬币完成,即必须使用比其他硬币更多的硬币 private static int ReturnChange(double change, int[] Coins, int[] numCoins) { int i = 0;

我的函数ReturnChange应该接受change,coins数组int coins[]={100,50,20,10,5};p、 它们以美分为单位,numCoins数组int numCoins[]={10,10,10,10};然后计算变化。零钱应该是硬币。如果硬币完成,即必须使用比其他硬币更多的硬币

private static int ReturnChange(double change, int[] Coins, int[] numCoins) {

    int i = 0;
    int totalcoins = 0;
    change = change * 100;
    do {

        totalcoins++;
        change -= 100;

    } while (change >= 100);
    System.out.println("1 dollar x " + totalcoins + "");
    i++;
    int coinsfifty = 0;
    do {
        coinsfifty++;
        change -= 50;

    } while (change >= 50);
    System.out.println("50 cents x " + coinsfifty + "");
    i++;
    int coinstwenty = 0;
    do {
        coinstwenty++;
        change -= 20;

    } while (change >= 20);
    System.out.println("20 cent x " + coinstwenty + "");
    i++;
    int coinsten = 0;
    do {
        coinsten++;
        change -= 10;

    } while (change >= 10);
    System.out.println("10 cent x " + coinsten + "");
    i++;
    int coinsfive = 0;
    do {

        coinsfive++;
        change -= 5;
    } while (change >= 5);
    System.out.println("5 cent x " + coinsfive + "");
    return 0;

}
4作为更改传递时的输出为:

1美元x4美元 50美分x1 20美分x1 10美分x1 5分x1

以2.5为例传递时的输出发生变化:

1美元x2 50美分x1 20美分x1 10美分x1 5分x1

问题是:
需要其他值,如50美分等,在输出1中显示零,在输出2中显示0表示20美分以上,而不是1。

好吧,您不应该使用do while。 这在逻辑上是错误的,因为“do”中的代码总是至少运行一次

只需将您的do whiles替换为while like:

do {
    totalcoins++;
    change -= 100;
} while (change >= 100);


我发誓我用了,虽然它不工作,我再次使用后,你提到它的工作!!!我想我没有去掉分号。谢谢你的回复。愿上帝增加你的知识。
while(change >= 100) {
    totalcoins++;
    change -= 100;
}