硬币数学转换问题

我正在编写一个程序,将便士转换成正确数量的二十,数十,五,美元,四分之一,硬币,镍币和便士。 我已经得到了程序的第一部分正确但是一旦我得到了美元,我的数学输出的数据并不正确。 有帮助吗?

import java.util.Scanner; public class assignment1 { public static void main(String[] args) { Scanner inputReader = new Scanner(System.in); int num, remainder; System.out.print("Enter a value as a number of pennies: "); num = inputReader.nextInt(); remainder = num % 2000; System.out.println(num + " pennies is equal to:"); System.out.println((num / 2000) + " twenties"); System.out.println((remainder / 1000) + " tens"); System.out.println(((remainder % 1000) / 500) + " fives"); System.out.println((((remainder % 1000) % 500) / 100) + " dollars"); System.out.println(((((remainder % 1000) % 500) % 100) / 25) + " quarters"); System.out.println(((((remainder % 1000) % 500) % 100) % 25) / 10 + " dimes"); System.out.println((((((remainder % 1000) % 500) % 100) % 25) % 10) / 5 + " nickels"); System.out.println(((((((remainder % 1000) % 500) % 100) % 25) % 10) % 5) / 1 + " pennies"); } } 

好。 我明白你做了什么。 让我们开始吧

 System.out.println((num / 2000) + "twenties"); System.out.println((remainder / 1000) + "tens"); System.out.println((remainder / 1000) % 500 + "fives"); System.out.println((((remainder / 1000) % 500) % 100) + "dollars"); 

第一行是正确的。 但是你不能使用简单的余数来保持运行记录。 此外, numremainder是可怕的变量名。 对于正在运行的记录,你可以做这样的事情,

 System.out.print("Enter a value as a number of pennies: "); int pennies = inputReader.nextInt(); System.out.println(pennies + " pennies is equal to:"); int twenties = pennies / 2000; pennies -= twenties * 2000; int tens = pennies / 1000; pennies -= tens * 1000; int fives = pennies / 500; pennies -= fives * 500; int dollars = pennies / 100; pennies -= dollars * 100; int quarters = pennies / 25; pennies -= quarters * 25; int dimes = pennies / 10; pennies -= dimes * 10; int nickels = pennies / 5; pennies -= nickels * 5; System.out.printf("%d twenties, %d tens, %d fives, %d dollars, " + "%d quarters, %d dimes, %d nickels, %d pennies%n", twenties, tens, fives, dollars, quarters, dimes, nickels, pennies); 

这将输出(以5869作为输入),

 5869 pennies is equal to: 2 twenties, 1 tens, 1 fives, 3 dollars, 2 quarters, 1 dimes, 1 nickels, 4 pennies 

或者(如果你想要一个非运行的计数器),只需从便士中删除减法

 System.out.println(pennies + " pennies is equal to:"); int twenties = pennies / 2000; int tens = pennies / 1000; int fives = pennies / 500; int dollars = pennies / 100; int quarters = pennies / 25; int dimes = pennies / 10; int nickels = pennies / 5; System.out.printf("%d twenties, %d tens, %d fives, %d dollars, " + "%d quarters, %d dimes, %d nickels, %d pennies%n", twenties, tens, fives, dollars, quarters, dimes, nickels, pennies); 

5869你得到了

 5869 pennies is equal to: 2 twenties, 5 tens, 11 fives, 58 dollars, 234 quarters, 586 dimes, 1173 nickels, 5869 pennies 

仅使用数学运算符即可实现。 在您将遇到的大多数其他编程任务中,您应该使用控制结构来完成算法中的步骤。

处理

  1. 从便士的数量开始,
  2. 除以2000(20美元的价值)并分配给一个新的变量,
  3. 减去二十几岁的便士数量,
  4. 重复后续面额(10美元钞票,5美元钞票等)

我认为问题在于,每次分割数字时,它都会返回一个新数字作为整数。 因此,如果你将100便士除以1000就会返回0,所以无论你做什么,你的数学剩余部分都会返回0。

基于您的代码的示例:

 //assuming the input is 100 pennies the first remainder/1000 will equal to 0 System.out.println(((((((remainder / 1000) % 500) % 100) % 25) % 10) %5) % 1 + "pennies"); 

你没有指定你必须使用模数,所以我假设你不是。 这就是我要做的:

 System.out.println(num + " pennies is equal to:"); System.out.println((num / 2000) + "twenties"); System.out.println((num / 1000) + "tens"); System.out.println((num / 500) + "fives"); System.out.println((num / 100) + "dollars"); System.out.println((num / 25) + "quarters"); System.out.println((num / 10) + "dimes"); System.out.println((num / 5) + "nickels"); System.out.println(num + "pennies");