简体   繁体   中英

Java change calculator output

I am writing a code that reads a real number from standard input as a double, then takes that value and prints out the least amount of bills and coin it takes to make that amount. I am not sure how to get the code to print out the amount with pluralizing certain amounts when necessary. If someone could chime in I would really appreciate it!

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 + "$100 bills");
         amount = amount % 100;
         }
     if(amount >= 50)
         {
         tmp = (int) amount/50;
         System.out.println (tmp + "$50 bills");
         amount = amount % 50;
         }
    if(amount >= 20)
    {
        tmp = (int) amount/50;
        System.out.println (tmp + "$20 bills");
        amount = amount % 20;
    }
    if(amount >= 10)
    {
        tmp= (int) amount/50;
        System.out.println (tmp + "$10 bills");
        amount = amount % 10;
    }
    if(amount >= 5)
    {
        tmp = (int) amount/5;
        System.out.println (tmp + "$5 bills");
        amount = amount % 5;
    }
    if(amount >= 1)
    {
        tmp = (int) amount/1;
        System.out.println (tmp + "$1 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;
    }
}
}

Your code is mostly right. Its just that when amount is >= 20, then you have

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

Instead it should be

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

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

Apart from this, you need to have logic to print bill or bills(in case of multiple bills) and have similar logic for quater(s), dime(s), nickel(s) & 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 + "$100 bills");
     } else{
         System.out.println (tmp + "$100 bill");
     }
     amount = amount % 100;
 }

You should change the code logic to print out the amount with pluralizing certain amounts. You could try the below code, it may help you

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 + "$100 bills");
         amount = amount % 100;
         }
     if(amount >= 50)
         {
         tmp = (int) amount/50;
         System.out.println (tmp + "$50 bills");
         amount = amount % 50;
         }
    if(amount >= 20)
    {
        tmp = (int) amount/20;
        System.out.println (tmp + "$20 bills");
        amount = amount % 20;
    }
    if(amount >= 10)
    {
        tmp= (int) amount/10;
        System.out.println (tmp + "$10 bills");
        amount = amount % 10;
    }
    if(amount >= 5)
    {
        tmp = (int) amount/5;
        System.out.println (tmp + "$5 bills");
        amount = amount % 5;
    }
    if(amount >= 1)
    {
        tmp = (int) amount/1;
        System.out.println (tmp + "$1 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;
    }
}
}

You are using wrong divisor. Use proper one like:

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

and not this:

tmp = (int) amount/50;

Here is what you can do print result in your format:

double tmp;
            String result = "Amount: ";

            if(amount >= 100)
            {
                tmp = (int) amount/100;
                result = result + (int)tmp + "$100 bills ";
                amount = amount % 100;
            }
            if(amount >= 50)
            {
                tmp = (int) amount/50;
                result = result + (int)tmp + "$50 bills ";
                amount = amount % 50;
            }
            if(amount >= 20)
            {
                tmp = (int) amount/20;
                result = result + (int)tmp + "$20 bills ";
                amount = amount % 20;
            }
            if(amount >= 10)
            {
                tmp= (int) amount/10;
                result = result + (int)tmp + "$10 bills ";
                amount = amount % 10;
            }
            if(amount >= 5)
            {
                tmp = (int) amount/5;
                result = result + (int)tmp + "$5 bills ";
                amount = amount % 5;
            }
            if(amount >= 1)
            {
                tmp = (int) amount/1;
                result = result + (int)tmp + "$1 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);
        }

Here is sample run:

Enter amount) 124 Amount: 1$100 bills 1$20 bills 4$1 bills

You can do slight modifications as per your requirements.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM