I'm currently in an online highschool course that is teaching me the basics of programming in java. My current assignment is:
I've written up a small portion of what I think the code should look like but I believe that I'm over-complicating what I've written so-far. If anyone has a few minutes to look over the below code and possibly suggest an easier way to complete this assignment it would be very much appreciated.Create a Java program that will:
1. Ask the user to input an amount of money as an integer value. Example: 123
2. Display the coins necessary to make up the amount entered by the user.
For example, if the user enters the integer value 123, your program
should output:
toonies = 0
loonies = 1
quarters = 0
dimes = 2
nickles = 0
pennies = 3
(I'm allowed to get help on these questions as long as I write the code myself.)
import java.io.*; //tells Java input will be used class Assignment4Question3 { public static void main (String[] args) throws IOException { InputStreamReader inStream = new InputStreamReader (System.in); BufferedReader userInput = new BufferedReader (inStream); String inputData; //Creates a string variable called 'inputData'. System.out.println ("Enter an amount of money as an interger. (Example: 123 = $1.23):"); inputData = userInput.readLine ( ); //Places input into inputData string variable. int pennies = 0; int nickles = 0; int dimes = 0; int quarters = 0; int loonies = 0; int toonies = 0; int intInputData = Integer.parseInt(inputData); pennies = (intInputData % 10); if (pennies >= 5) { nickles = 1; pennies = (pennies - 5); dimes = ((((intInputData % 100) - (pennies)) - 5) / 10); if (dimes == 3) { nickles = (nickles + 1); dimes = (dimes - 3); quarters = 1; if (nickles == 2) { nickles = (nickles - 2); dimes = (dimes + 1); } } else if (dimes == 5) { dimes = (dimes - 5); quarters = 2; } else if (dimes >= 8) { nickles = (nickles + 1); dimes = (dimes - 7); quarters = (quarters + 3); if (nickles == 2) { nickles = (nickles - 2); dimes = (dimes + 1); } } } System.out.println("Pennies = "+pennies+""); System.out.println("Nickles = "+nickles+""); System.out.println("Dimes = "+dimes+""); System.out.println("Quarters = "+quarters+""); } }
At the moment the above is crudly calculating the pennies, nickles, dimes and quarters with a few errors but it mostly works.