【问题标题】:Flaw in my Change Calculator?我的零钱计算器有缺陷?
【发布时间】:2015-09-08 07:19:01
【问题描述】:

我一直在开发一个找零计算器,它将输入 1) 客户姓名、2) 商品描述、3) 商品的购买价格和 4) 客户投标的金额。输出客户名称、描述、成本(带小数点)、投标金额(带小数点)和一美元钞票、25美分硬币、5美分硬币和便士的变化。

这是我目前所拥有的,当我编译和运行它时,当它得到一角硬币和便士时,它给了我错误的变化量。

/* CCTPG22 : Assignment02_ Changer
 * @author   Kevin Trujillo
 * @version  8/30/2014
 * Purpose:  Your program will take input for the customer name, 
     the item description, purchase price of an item and
     the amount tendered by the customer. Output the customer
     ame, description, cost (with decimal point), amount tendered 
     (with decimal point) and change in one dollar bills, quarters, 
     ickels, dimes and pennies.  
 * Status: In Progress    
 */
import java.util.*;  

public class Assignment02_Changer
{
   public static void main(String[] args)
   {

     String costumer_Name ;                                                  // Variables Declared
     String item_Bought ;
     int amt_DUE ;
     int amt_PAID ;

          final int pennies_per_dollar = 100 ;
          final int nickles_per_dollar = 20 ;
          final int dimes_per_dollar = 10 ;
          final int quarters_per_dollar = 4 ;
          final int pennies_value = 1 ;

        int change_due ;
        int change_left ;
        int dollars_returned ;                                              // Amt of dollars  returned
        int quarters_returned ;                                             // Amt of Quarters returned
        int dimes_returned ;                                                // Amt of dimes    returned
        int nickles_returned ;                                              // Amt of nickles  returned
        int pennies_returned ;                                              // Amt of pennies  returned

     Scanner in = new Scanner(System.in);                                   // Intoduces Input Operator 

     System.out.print("Change making program by Kevin Trujillo. \n") ;      // First Series of Outputs and Inputs (self-explanatory)

     System.out.print("What is the customer's name?  ") ;
     costumer_Name = in.nextLine() ;

     System.out.print("Provide one line to describe the item: ") ;
     item_Bought = in.nextLine() ;

     System.out.print("Please enter the item price in pennies: ") ;
     amt_DUE = in.nextInt() ;

     System.out.print("Please enter the amount tendered in cents ") ;
     amt_PAID = in.nextInt() ;

        System.out.print(costumer_Name + " bought a " + item_Bought + " for " + amt_DUE ) ;
        System.out.print(" and tendered " + amt_PAID + " in cents. \n") ;

                change_due = amt_DUE - amt_PAID ;                           // caluc. total AMT of change returned     
                dollars_returned = change_due / pennies_per_dollar ;            // calcu. AMT of dollars returned

                change_left = change_due % pennies_per_dollar ;                 // finds remainder due
                change_due = change_left ;

                quarters_returned = change_due / quarters_per_dollar ;          // calcu. AMT of quarters returned

                change_left = change_due % quarters_per_dollar ;                // finds remainder due
                change_due = change_left ;

                dimes_returned = change_due / dimes_per_dollar ;                // finds remainder due

                change_left = change_due % dimes_per_dollar ;                   // calcu. AMT of dimes returned
                change_due = change_left ;

                nickles_returned = change_due / nickles_per_dollar ;            // finds remainder due

                change_left = change_due % nickles_per_dollar ;                 // calcu. AMT of nickles returned
                change_due = change_left ;

                dimes_returned = change_due / dimes_per_dollar ;                // finds remainder due

                change_left = change_due % dimes_per_dollar ;                    // caluc. AMT of dimes returned
                change_due = change_left ;  

                pennies_returned = change_due / pennies_per_dollar ;            // finds remainder due


                     System.out.print(costumer_Name +  "'s change is: \n" );
                     System.out.printf("      " + dollars_returned + " one-dollar bill (s)\n") ;
                     System.out.printf("      " + quarters_returned + " quarter (s)\n" ) ;
                     System.out.printf("      " + dimes_returned + " dime (s)\n") ;
                     System.out.printf("      " + nickles_returned + " nickle (s)\n") ;
                     System.out.printf("      " + pennies_returned + " penny (ies)\n") ;
                     System.out.print("Thank you for your business. Come back soon " + costumer_Name + "!\n" ) ;



   }                       // braces for the main class


}                          // braces for the program

所以我的问题是我的代码中是否出现算术错误,或者您是否能看到我看不到的东西。

This is an example of what the program should look like. 
Change making program by Don Smith
What is the customer's first name? Bob
Provide one line to describe the item: Chem Book
Please enter the item price in cents: 3458
Please enter the amount tendered in cents: 4000
Bob bought a Chem Book for 34.58 and tendered 40.00
Bob's change is: 
     5 one-dollar bill(s)
     1 quarter(s)
     1 dime(s)
     1 nickel(s)
     2 penny(ies)

Thanks for your business.  Come back soon.

【问题讨论】:

  • 以您的真实姓名在这里发布您的作业可能不是最好的主意。

标签: java math calculator


【解决方案1】:

通过查看您的代码,我可以发现以下内容:

在你得到一美元钞票的金额和剩下的美分(小于 100)之后,你将它除以 4(一美元的四分之一数),你应该将它除以 25,然后你应该向下遵循相同的逻辑。简而言之,您应该除以任何四分之一/一角/镍/一分钱的美分数,而不是除以其中有多少能赚到一美元。

【讨论】:

    【解决方案2】:

    您的代码和输出未同步。标准操作程序是不同的,还有一些数学错误。可能是您发布了您尝试过的旧代码。但这些更改应该会对您有所帮助。

    final int pennyValue = 1 ;//Change occurrences of these subsequently
    final int nicklesValue = 5 ;
    final int dimesValue = 10 ;
    final int quartersValue = 25 ;
    

    。 . .

    change_due = amt_PAID - amt_DUE ;//You have it like amt_DUE  - amt_PAID gives negative numbers
    

    可能有错别字。但这应该可以解决。

    【讨论】:

      【解决方案3】:

      将您的 final static 变量更改为:

      int dollar = 100;
      int quarter = 25;
      int dime = 10;
      int nickel = 5;
      int penny = 1;
      

      然后使用:

      int change = paid - due;
      int dollar_c = change / dollar;
      change = change % dollar;
      int quarter_c = change / quarter;
      change = change % quarter;
      int dime_c = change / dime;
      change = change % dime;
      int nickel_c = change % nickel;
      change = change % nickel;
      

      其中dollar_c 是您的dollars_returnedchangepennies_returned

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-09-23
        • 1970-01-01
        • 1970-01-01
        • 2013-06-09
        • 2019-02-28
        • 2020-06-06
        • 2011-01-16
        相关资源
        最近更新 更多