Java 更改计算器输出

Java change calculator output

我正在编写一个代码,从标准输入中读取一个双精度实数,然后获取该值并打印出达到该金额所需的最少纸币和硬币。我不确定如何让代码在必要时打印出一定金额的复数形式。如果有人可以插话,我将不胜感激!

import java.util.Scanner;
import java.lang.Math;
public class Changecalc {
public static void main(String[] args) {
     Scanner input = new Scanner(System.in);
     System.out.println("Enter amount) ");
     double amount = input.nextDouble();
     int amountInt = (int) (amount * 100);

     double tmp;
     if(amount >= 100)
         {
         tmp = (int) amount/100;
         System.out.println (tmp + "0 bills");
         amount = amount % 100;
         }
     if(amount >= 50)
         {
         tmp = (int) amount/50;
         System.out.println (tmp + " bills");
         amount = amount % 50;
         }
    if(amount >= 20)
    {
        tmp = (int) amount/50;
        System.out.println (tmp + " bills");
        amount = amount % 20;
    }
    if(amount >= 10)
    {
        tmp= (int) amount/50;
        System.out.println (tmp + " bills");
        amount = amount % 10;
    }
    if(amount >= 5)
    {
        tmp = (int) amount/5;
        System.out.println (tmp + " bills");
        amount = amount % 5;
    }
    if(amount >= 1)
    {
        tmp = (int) amount/1;
        System.out.println (tmp + " bills");
        amount = amount % 1;
    }
    if(amount >= .25)
    {
        tmp = (int) amount/.25;
        System.out.println (tmp + "quater");
        amount = amount % .25;
    }
    if(amount >= .10)
    {
        tmp = (int) amount/.10;
        System.out.println (tmp + "dime");
        amount = amount % .10;
    }
    if(amount >= .05)
    {
        tmp = (int) amount/.05;
        System.out.println (tmp + "nickel");
        amount = amount % .05;
    }
    if(amount >= .01)
    {
        tmp = (int) amount/.01;
        System.out.println (tmp + "penny");
        amount = amount % 01;
    }
}
}

您的代码大部分是正确的。只是当 amount >= 20 时,你有

if(amount >= 20)
{
    tmp = (int) amount/50;
    System.out.println (tmp + " bills");
    amount = amount % 20;
}

Instead it should be

if(amount >= 20)
{
    tmp = (int) amount/20;
    System.out.println (tmp + " bills");
    amount = amount % 20;
}

Similar issue is when amount is >=10. You need to correct that as well.

除此之外,您需要具有打印帐单或帐单的逻辑(在多张帐单的情况下),并且具有类似的逻辑用于四分之一、一角硬币、五分硬币和 penny/pennies


For pluralising you can use something similar to following

if(amount >= 100){
     tmp = (int) amount/100;         
     if(tmp > 1.0){
         System.out.println (tmp + "0 bills");
     } else{
         System.out.println (tmp + "0 bill");
     }
     amount = amount % 100;
 }

您使用了错误的除数。使用合适的,例如:

if(amount >= 20)
{
   tmp = (int) amount/20;
   System.out.println (tmp + " bills");
   amount = amount % 20;
}
if(amount >= 10)
{
   tmp= (int) amount/10;
   System.out.println (tmp + " bills");
   amount = amount % 10;
}

而不是这个:

tmp = (int) amount/50;

您可以按照您的格式打印结果:

double tmp;
            String result = "Amount: ";

            if(amount >= 100)
            {
                tmp = (int) amount/100;
                result = result + (int)tmp + "0 bills ";
                amount = amount % 100;
            }
            if(amount >= 50)
            {
                tmp = (int) amount/50;
                result = result + (int)tmp + " bills ";
                amount = amount % 50;
            }
            if(amount >= 20)
            {
                tmp = (int) amount/20;
                result = result + (int)tmp + " bills ";
                amount = amount % 20;
            }
            if(amount >= 10)
            {
                tmp= (int) amount/10;
                result = result + (int)tmp + " bills ";
                amount = amount % 10;
            }
            if(amount >= 5)
            {
                tmp = (int) amount/5;
                result = result + (int)tmp + " bills ";
                amount = amount % 5;
            }
            if(amount >= 1)
            {
                tmp = (int) amount/1;
                result = result + (int)tmp + " bills ";
                amount = amount % 1;
            }
            if(amount >= .25)
            {
                tmp = (int) amount/.25;
                result = result + (int)tmp + " qurters ";
                amount = amount % .25;
            }
            if(amount >= .10)
            {
                tmp = (int) amount/.10;
                result = result + (int)tmp + " dimes ";
                amount = amount % .10;
            }
            if(amount >= .05)
            {
                tmp = (int) amount/.05;
                result = result + (int)tmp + " nickels ";
                amount = amount % .05;
            }
            if(amount >= .01) {
                tmp = (int) amount / .01;
                result = result + (int)tmp + " penny ";
                amount = amount % 01;
            }
            System.out.println(result);
        }

这是示例 运行:

Enter amount) 124 Amount: 10 bills 1 bills 4 bills

您可以根据自己的要求稍作修改。

您应该更改代码逻辑以打印出金额的复数形式。您可以尝试下面的代码,它可能对您有所帮助

import java.util.Scanner;
import java.lang.Math;
class Changecalc {
public static void main(String[] args) {
     Scanner input = new Scanner(System.in);
     System.out.println("Enter amount) ");
     double amount = input.nextDouble();
     int amountInt = (int) (amount * 100);

     double tmp;
     if(amount >= 100)
         {
         tmp = (int) amount/100;
         System.out.println (tmp + "0 bills");
         amount = amount % 100;
         }
     if(amount >= 50)
         {
         tmp = (int) amount/50;
         System.out.println (tmp + " bills");
         amount = amount % 50;
         }
    if(amount >= 20)
    {
        tmp = (int) amount/20;
        System.out.println (tmp + " bills");
        amount = amount % 20;
    }
    if(amount >= 10)
    {
        tmp= (int) amount/10;
        System.out.println (tmp + " bills");
        amount = amount % 10;
    }
    if(amount >= 5)
    {
        tmp = (int) amount/5;
        System.out.println (tmp + " bills");
        amount = amount % 5;
    }
    if(amount >= 1)
    {
        tmp = (int) amount/1;
        System.out.println (tmp + " bills");
        amount = amount % 1;
    }
    if(amount >= .25)
    {
        tmp = (int) amount/.25;
        System.out.println (tmp + "quater");
        amount = amount % .25;
    }
    if(amount >= .10)
    {
        tmp = (int) amount/.10;
        System.out.println (tmp + "dime");
        amount = amount % .10;
    }
    if(amount >= .05)
    {
        tmp = (int) amount/.05;
        System.out.println (tmp + "nickel");
        amount = amount % .05;
    }
    if(amount >= .01)
    {
        tmp = (int) amount/.01;
        System.out.println (tmp + "penny");
        amount = amount % .01;
    }
}
}