我一直致力于更改计算器将输入1)客户名称,2)项目描述,3)项目的购买价格和4)客户提交的金额。输出客户名称,描述,成本(带小数点),投标金额(带小数点)并更改一美元钞票,四分之一,镍币,硬币和便士。
这是我到目前为止所做的,当我编译并运行它时,它给了我错误的变化量,当它达到一角硬币和便士时。
/* 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.
答案 0 :(得分:1)
通过查看您的代码,我可以发现以下内容:
在您获得一美元钞票的金额并且您获得剩余的美分(小于100)之后,您将其除以4(一美元的季度数),您应该将其除以25,然后您应该向下遵循相同的逻辑。简而言之,你应该除以任何季度/硬币/镍/便士的分数,而不是它们中有多少分为1美元。
答案 1 :(得分:0)
您的代码和输出未同步。 SOP是不同的,也有一些数学错误。可能是您发布了您尝试过的旧代码。但是这些改变应该对你有帮助。
while(<DATA>) {
chomp;
print "$1\n" if /xh = (\d+)/
}
__DATA__
abc.txt
a = 1 b = 2 c = 3 d = 4
+xh = 10 e = 9 f = 11
+some lines
+xh = 12 g=14
+some lines
some lines
+xh = 13 i=15 j=20
some lines
。 。
final int pennyValue = 1 ;//Change occurrences of these subsequently
final int nicklesValue = 5 ;
final int dimesValue = 10 ;
final int quartersValue = 25 ;
可能有拼写错误。但这应该解决。
答案 2 :(得分:0)
将您的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_returned
,而change
是pennies_returned
。