我正在完成一项任务,而且我已经编写了大部分代码。该程序要求用户输入美元和美分的金额。然后,该计划将把金额分成不同的面额。我之前得到了它的工作,但在格式化我的打印语句并做了一些测试之后,我意识到没有为硬币打印的值。任何建议或意见将不胜感激。下面是我输出的代码。
import java.util.*;
public class BillBreaker {
// Instantiate Scanner class for reading keyboard input
public static Scanner kbd = new Scanner(System.in);
public static void main(String[] args) {
// TODO Auto-generated method stub
// instantiate Scanner class for reading keyboard input
double orgAmount;
int orgPennies; // pennies part of the amount
int remPennies;
long orgDollars;
// variables for bills of denominations.
long hundreds;
int fifties, twenties, tens, fives, twos, ones;
//variables for keeping track of coins
int pennies, nickels, dimes, quarters;
// print greeting and prompt user for input.
System.out.println("Given an amount in dollars and cents, I will find");
System.out.println("a combination of bills and coins:\n");
System.out.println("Enter a dollar amount including cents.");
// read amount of dollars and pennies.
orgAmount = kbd.nextDouble();
// printf is used to format user input into dollars and cents with comma delimiter
System.out.printf("Amount you entered in Dollars and cents: $%,.2f\n ",orgAmount);
orgDollars = (long)orgAmount;
System.out.printf("\nDollars Part of Original Amount: $%,d\n", orgDollars);
//extract the pennies from in orgAmount
orgPennies = (int)((orgAmount % 100));
System.out.println("pennies part of orginal amount: " + orgPennies);
//Extract and print the number of \$100 bills from the dollar amount.
//Reduce the dollar amount to account for \$100 bills
hundreds = (long)orgAmount/100;
orgAmount = orgAmount%100;
System.out.printf ("Number of $100 bills: %,d\n",hundreds);
//Extract and print the number of \$50 bills. Reduce the dollar amount
//to account for \$50 bills.
fifties = (int)orgAmount/50;
orgAmount = orgAmount%50;
System.out.println ("Number of $50 bills: " + fifties);
//Extract and print the number of \$20 bills. Reduce the dollar amount
//to account for \$20 bills.
twenties = (int) orgAmount/20;
orgAmount = orgAmount%20;
System.out.println ("Number of $20 bills: " + twenties);
//Extract and print the number of \$10 bills. Reduce the dollar amount
//to account for \$10 bills
tens = (int) orgAmount/10;
orgAmount = orgAmount%10;
System.out.println ("Number of $10 bills: " + tens);
//Extract and print the number of \$5 bills. Reduce the dollar amount
//to account for \$5 bills
fives = (int) orgAmount/5;
orgAmount = orgAmount%5;
System.out.println ("Number of $5 bills: " + fives);
//Extract and print the number of \$2 bills. Reduce the dollar amount
//to account for \$2 bills
twos = (int) orgAmount/2;
orgAmount = orgAmount%2;
System.out.println ("Number of $2 bills: " + twos);
//Extract and print the number of \$1 bills. Reduce the dollar amount
//to account for \$1 bills
ones = (int) orgAmount/1;
orgAmount = orgAmount%1;
System.out.println ("Number of $1 bills: " + ones);
//Extract and print the number of quarters from the number of pennies.
//Reduce the number of pennies to account for the quarters
quarters = (int) (orgAmount/0.25);
orgAmount = orgAmount%.25;
System.out.println ("Number of Quarters: " + quarters);
//Extract and print the number of dimes from the number of pennies.
//Reduce the number of pennies to account for the dimes
dimes = (int) (orgAmount/0.10);
orgAmount = orgAmount%.10;
System.out.println ("Number of Dimes: " + dimes);
//Extract and print the number of nickels from the number of pennies.
//Reduce the number of pennies to account for the nickels
nickels = (int) (orgAmount/0.05);
orgAmount = orgAmount%.05;
System.out.println ("Number of Nickels: " + nickels);
//Extract and print the number of pennies and terminate the program
pennies = (int) (orgAmount/0.01);
orgAmount = orgAmount%.01;
System.out.println ("Number of Pennies: " + pennies);
}
}
我的输出1:起始量:323.55 这使我在下面的前3个测试输出中得到了正确的数量。
Given an amount in dollars and cents, I will find
a combination of bills and coins:
Enter a dollar amount including cents.
323.55
Amount you entered in Dollars and cents: $323.55
Dollars Part of Original Amount: $323
pennies part of orginal amount: 23
Number of $100 bills: 3
Number of $50 bills: 0
Number of $20 bills: 1
Number of $10 bills: 0
Number of $5 bills: 0
Number of $2 bills: 1
Number of $1 bills: 1
Number of Quarters: 2
Number of Dimes: 0
Number of Nickels: 1
Number of Pennies: 0
输出2:开始金额:
Given an amount in dollars and cents, I will find
a combination of bills and coins:
Enter a dollar amount including cents.
2335.32
Amount you entered in Dollars and cents: $2,335.32
Dollars Part of Original Amount: $2,335
pennies part of orginal amount: 35
Number of $100 bills: 23
Number of $50 bills: 0
Number of $20 bills: 1
Number of $10 bills: 1
Number of $5 bills: 1
Number of $2 bills: 0
Number of $1 bills: 0
Number of Quarters: 1
Number of Dimes: 0
Number of Nickels: 1
Number of Pennies: 2
输出3:开始金额:9879823.92
Given an amount in dollars and cents, I will find
a combination of bills and coins:
Enter a dollar amount including cents.
9879823.92
Amount you entered in Dollars and cents: $9,879,823.92
Dollars Part of Original Amount: $9,879,823
pennies part of orginal amount: 23
Number of $100 bills: 98,798
Number of $50 bills: 0
Number of $20 bills: 1
Number of $10 bills: 0
Number of $5 bills: 0
Number of $2 bills: 1
Number of $1 bills: 1
Number of Quarters: 3
Number of Dimes: 1
Number of Nickels: 1
Number of Pennies: 1
然而,在一定数量的情况下,产量主要以便士的价格减少一个 现在修改后的代码在变量启动期间发生了变化,但我不确定我是否理解是否需要使用orgPennies而不是所有的orgAmount?
这是错误输出,便士少了一个。
输出4:开始金额:111.11
Given an amount in dollars and cents, I will find
a combination of bills and coins:
Enter a dollar amount including cents.
111.11
Amount you entered in Dollars and cents: $111.11
Dollars Part of Original Amount: $111
pennies part of orginal amount: 11
Number of $100 bills: 1
Number of $50 bills: 0
Number of $20 bills: 0
Number of $10 bills: 1
Number of $5 bills: 0
Number of $2 bills: 0
Number of $1 bills: 1
Number of Quarters: 0
Number of Dimes: 1
Number of Nickels: 0
Number of Pennies: 0
也许我一直在看这个太长时间,需要离开一分钟。我确信它会非常明显,但同样,任何帮助都会受到赞赏。
答案 0 :(得分:1)
orgAmount = (long)orgAmount;
System.out.printf("\nDollars Part of Original Amount: $%,.0f\n", orgAmount);
//extract the pennies from in orgAmount
orgPennies = (int)((orgAmount % 100) * 100);
System.out.println("pennies part of orginal amount: " + orgPennies);
在尝试将值放入orgPennies之前,您似乎要从orgAmount中删除小数位值,而orgPennies不应该有“* 100”,然后您忘记使用orgPennies而不是orgAmount来计算数量便士。
答案 1 :(得分:0)
您正在使用双倍来保存输入金额并进行计算。你不能指望精确到双分。你必须使用BigDecimal来持有大量资金。
这是我将你的双打更改为BigDecimals之后的测试运行。
%doctest_mode
以下是代码:
Given an amount in dollars and cents, I will find
a combination of bills and coins:
Enter a dollar amount including cents.
9879823.92
Amount you entered in Dollars and cents: $9,879,823.92
Dollars Part of Original Amount: $9,879,823
pennies part of orginal amount: 92
Number of $100 bills: 98,798
Number of $50 bills: 0
Number of $20 bills: 1
Number of $10 bills: 0
Number of $5 bills: 0
Number of $2 bills: 1
Number of $1 bills: 1
Number of Quarters: 3
Number of Dimes: 1
Number of Nickels: 1
Number of Pennies: 2