本文介绍了硬币数学转换问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我目前正在编写一个程序,它会将便士转换成正确的二十,十,五,美元,季度,迪斯,镍和便士的数量。我已经得到了程序的第一部分正确,但一旦我得到美元,我的数学不输出正确的金额。任何帮助? 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(输入一个值作为一个数字:); 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(((remaining%1000)/ 500)+fives); System.out.println((((remainder%1000)%500)/ 100)+dollars); System.out.println(((((remaining%1000)%500)%100)/ 25)+quarters); System.out.println(((((remaining%1000)%500)%100)%25)/ 10 +dimes); System.out.println(((((remaining%1000)%500)%100)%25)%10)/ 5 +nickels); System.out.println((((((remaining%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((((remaining / 1000)%500)%100)+dollars); 第一行是正确的。但是你不能使用简单的余数来保持运行的计数。此外, num 和 remainder 是可怕的变量名。您可以为运行的计数器执行这样的操作, System.out.print(输入一个值, :); 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个二十,%d个,%d个fives,%d美元, +%d个季度,%d个dimes,%d个镍,%d个n,二十, tens,fives,dollars,quarters,dimes,nick,pennies); 这将输出(用5869作为输入), 5869 pennies等于: 2二十,1 10,1 fives,3 dollars,2 quarter,1 dimes,1 nick,4 pennies 或者(如果你想要一个不运行的tally),只需从penny中删除 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个二十,%d个,%d个fives,%d美元, +%d个季度,%d个dimes,%d个镍,%d个n,二十, tens,fives,dollars,quarters,dimes,nick,pennies); 对于5869,您可以获得 5869 pennies等于: 2二十,5十,11五年,58美元,234季,586迪斯,1173镍,5869便士 i'm currently coding a program that will convert pennies into the correct number of Twenties, tens, fives, dollars, quarters, dimes, nickels, and pennies. I've gotten the first part of the program correct but once i get to dollars my math is not outputting the correct amount. Any help?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");}} 解决方案 Ok. I see what you've done. Let's start withSystem.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");The first line is correct. But you cannot use a simple remainder to keep a running tally. Also, num and remainder are terrible variable names. You can do something like this for a running tally,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);That would output (with 5869 as input),5869 pennies is equal to:2 twenties, 1 tens, 1 fives, 3 dollars, 2 quarters, 1 dimes, 1 nickels, 4 penniesOr (if you wanted a non-running tally), just remove the subtractions from pennies likeSystem.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);And for 5869 you get5869 pennies is equal to:2 twenties, 5 tens, 11 fives, 58 dollars, 234 quarters, 586 dimes, 1173 nickels, 5869 pennies 这篇关于硬币数学转换问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-23 06:31