Java改变计算器输出

时间:2015-09-28 04:23:22

标签: java calculator computer-science

我正在编写一个代码,从标准输入中读取一个实数作为double,然后获取该值并打印出最少量的账单和硬币来赚取这个数量。我不知道如何在必要时将代码打印出多个特定金额的数据。如果有人可以插话,我会非常感激!

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;
    }
}
}

3 个答案:

答案 0 :(得分:1)

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

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

相反它应该是

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

类似的问题是当金额> = 10时。你需要纠正它   好。

除此之外,您需要有逻辑来打印账单或账单(如果是多个账单),并且具有类似于quater(s),dime(s),nickel(s)& s的逻辑。便士/便士

  

对于复数,您可以使用类似于以下内容的内容

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;
 }

答案 1 :(得分:1)

您应该更改代码逻辑以打印多个特定金额的金额。您可以尝试以下代码,它可能会帮助您

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;
    }
}
}

答案 2 :(得分:0)

你使用了错误的除数。使用适当的一个,如:

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;
}

而不是这个:

tmp = (int) amount/50;

您可以使用以下格式打印结果:

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);
        }

以下是样本运行:

  

输入金额)124金额:1 $ 100账单1 $ 20账单4 $ 1账单

您可以根据自己的要求进行细微修改。