作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在做一项作业,并且在很大程度上我已经编写了大部分代码。该程序要求用户输入美元和美分的金额。然后,该程序会将金额分成不同的面额。我之前就让它工作了,但是在格式化我的打印语句并做了一些测试之后,我意识到没有为硬币打印任何值。任何意见或建议将不胜感激。下面是我的代码和输出。
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
也许我已经看这个问题太久了,需要离开一会儿。我确信它会非常明显,但同样,任何帮助将不胜感激。
最佳答案
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);
看起来您正在从 orgAmount 中删除小数位值,然后再尝试将值放入 orgPennies 中,orgPennies 不应该有“* 100”,然后您忘记使用 orgPennies 而不是 orgAmount 来计算便士。
关于java - 货币面额计划,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35544900/
我有一个像这样的对象: var currencyTypes = { NOK: {value:1.00000, name: "Norske kroner", denomination: "k
我是一名优秀的程序员,十分优秀!