3 回答
TA贡献1966条经验 获得超4个赞
您的余数字段似乎以美分表示。
$100 = 10000
$20 = 2000
$1 = 100
因此,四分之一应为 25,一角应为 10,五分镍应为 5。
还要更改此:
double pennies = (int) remainder;
对此:
long pennies = remainder;
修复扫描仪输入的 amount 并删除所有最终存储在 long 中的行的 (int) 转换:
public class Assignment04 {
public static void main(String[] args) {
Scanner stdin = new Scanner(System.in);
System.out.println("Enter your amount");
long remainder = Math.round(stdin.nextDouble() * 100 );
long hundreds = remainder / 10000;
remainder = remainder % 10000;
long fifties = remainder / 5000;
remainder = remainder % 5000;
long twenties = remainder / 2000;
remainder = remainder % 2000;
long tens = remainder / 1000;
remainder = remainder % 1000;
long fives = remainder / 500;
remainder = remainder % 500;
long ones = remainder / 100;
remainder = remainder % 100;
long quarters = remainder / 25;
remainder = remainder % 25;
long dimes = remainder / 10;
remainder = remainder % 10;
long nickels = remainder / 5;
remainder = remainder % 5;
long pennies = remainder;
System.out.println(hundreds + "hundred/s");
System.out.println(fifties + "fiftie/s");
System.out.println(twenties + "twentie/s");
System.out.println(tens + "ten/s");
System.out.println(fives + "five/s");
System.out.println(ones + "one/s");
System.out.println(quarters + "quarter/s");
System.out.println(dimes + "dime/s");
System.out.println(nickels + "nickel/s");
System.out.println(pennies + "cent/s");
}
}
TA贡献1773条经验 获得超3个赞
你可以使用 int 。而不是长。我希望这有帮助。谢谢
Scanner stdin = new Scanner(System.in);
int amount;
int remainder;
System.out.print("Enter amount:");
amount= (int) Math.round(stdin.nextDouble() * 100);
int hundreds= remainder / 1000;
if (hundreds > 0) {
remainder = remainder% 1000;
System.out.println(hundreds + "hundred/s");
}
int fifties = remainder / 1000;
if (fifties > 0) {
remainder = remainder% 1000;
System.out.println(fifties + "fiftie/s");
}
// number of twenties for remainder.
int twenties = change / 2000
if (twenties > 0) {
remainder= remainder % 2000; // this resets the value of remainder to
// the remainder after the twenties are
// calculated but only if there was at
// least enough to make one twenty
System.out.println(twenties + "twentie/s");
}
int tens = remainder / 1000;
if (tens > 0) {
remainder = remainder% 1000;
System.out.println(tens + " ten/s");
}
int fives = remainder/ 500;
if (fives > 0) {
remainder= remainder% 500;
System.out.println(fives + " five/s");
}
int ones = remainder/ 100;
if (ones > 0) {
remainder = remainder % 100;
System.out.println(ones + " one/s");
}
int quarters = remainder/ 25;
if (quarters > 0) {
remainder= remainder% 25;
System.out.println(quarters + " quarter/s");
}
int dimes = remainder/ 10;
if (dimes > 0) {
remainder = remainder % 10;
System.out.println(dimes + " dime/s");
}
int nickels = remainder/ 5;
if (nickels > 0) {
remainder= change % 5;
System.out.println(nickels + " nickel/s");
}
int pennies = remainder;
System.out.println(pennies + " cent/s");
}
}
添加回答
举报