Hi, everyone. I would like to take the time to thank you all in advance for the time you take to review and assist with my dilemma(s). This is my first post and I apologize if I make any errors pertaining to the prescribed forum guidelines. I appreciate any criticism!
I began my Java journey buy reading a free online pdf textbook about programming in Java. I am half way through and now have an elementary understanding of the basics of Java. I am trying to master the resources I have read through creating programs including each section. The program I am trying to code is a simple change counter where the user gives a value (i.e. $232.75) and the program gives them the breakdown of bills that will be given back in change. This is a similar concept as to what a modern cash register does. I have written the code and anytime the input consists of something other than 100's or 20's, the program displays a value of 0 for the rest of forms of money such as quarters, 10's, pennies...
import java.util.Scanner; public class Change { private static Scanner input; public static void main(String[] args) { input = new Scanner (System.in); double money; //User specified value to convert int change; //Converted "Money" to int type int hund, twend, tend, fived, oned, quarters, dimes, nickels, pennies; //The forms change can be given in int afthund, afttwend, afttend, aftfived, aftoned, aftquarters, aftdimes, aftnickels; //Money left over following each form of change System.out.println("How much money would you like me to give you change for?"); //Ask the user for a value money = input.nextDouble(); money = money * 100; //Multiply double "money" by 100 to later work with integers change = (int)(money); //Typecast double "money" to an int value hund = change / 10000; //Divide change by 10,000 (hund) -- hundred dollars afthund = change % 10000; //then get amount left over (afthund) -- after hundred dollars twend = afthund / 2000; //Divide afthund by 2000 (twend) afttwend = twend % 2000; //then get amount left over (afttwend) tend = afttwend / 1000; //Divide aftwend by 1000 (tend) afttend = tend % 1000; //then get amount left over (afttend) fived = afttend / 500; //Divide afttend by 500 (fived) aftfived = fived % 500; //then get amount left over (aftfived) oned = aftfived / 100; //Divide aftfived by 100 (oned) aftoned = oned % 100; //then get amount left over (aftoned) quarters = aftoned / 25; //Divide aftoned by 25 (quarters) aftquarters = quarters % 25; //then get amount left over (aftquarters) dimes = aftquarters / 10; //Divide aftquarters by 10 (dimes) aftdimes = dimes % 10; //then get amount left over (aftdimes) nickels = aftdimes / 5; //Divide aftdimes by 5 (nickels) aftnickels = nickels % 5; //then get amount left over (aftnickels) pennies = aftnickels; //Remainder is pennies System.out.print("Hundreds: " + hund); //Print hundreds System.out.println(); System.out.print("Twenties: " + twend); //Print twenties System.out.println(); System.out.print("Tens: " + tend); //Print tens System.out.println(); System.out.print("Fives: " + fived); //Print fives System.out.println(); System.out.print("Ones: " + oned); //Print ones System.out.println(); System.out.print("Quarters: " + quarters); //Print quarters System.out.println(); System.out.print("Dimes: " + dimes); //Print dimes System.out.println(); System.out.print("Nickels: " + nickels); //Print nickels System.out.println(); System.out.print("Pennies: " + pennies); //Print pennies System.out.println(); } }
I have provided as much documentation as possible. If you have any ideas for a solution or if I need to start over, please let me know.
Have a great day,
bzhagar