Results 1 to 6 of 6
Thread: Getting the remaining cents
- 12-30-2009, 12:35 AM #1
Member
- Join Date
- Dec 2009
- Posts
- 10
- Rep Power
- 0
Getting the remaining cents
Hi, I would like to know how to get the remaining cents from the program I've made..
Here's the program:
Java Code:import java.io.*; public class centsDollars { public static void main (String[]args) { BufferedReader input = new BufferedReader (new InputStreamReader(System.in)); String x=""; try { System.out.println("Enter amount in nickel: "); x = input.readLine(); int nickelin = Integer.parseInt(x); System.out.println("Enter amount in penny: "); x = input.readLine(); int penniesin = Integer.parseInt(x); double pennies = (nickelin*5)+(penniesin); double nickel = (pennies/5); System.out.println("You entered " +nickelin+ " nickels and " +penniesin+ " pennies.\n"); System.out.println("The total amount in penny is "+pennies+"."); System.out.println("The total amount in nickel is "+nickel+".\n"); double dollar = (pennies/100); System.out.println("The total amount is " +dollar+" Dollar." ); } catch (IOException e) { System.out.println("Error!"); } } }
Assuming I entered 3 nickels and 122 pennies.
This displays:
You entered 3 nickels and 122 pennies.
The total amount in penny is 137.0.
The total amount in nickel is 27.4.
The total amount is 1.37 Dollar.
How can I output the amount in dollars the same as this?:
The total amount is 1 Dollar and 37 Cents.
- 12-30-2009, 01:15 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Forget using double. Everything here is countable so use int and reserve double for things that you would measure rather than count.
Find out about the integer division and the remainder (%) operator. Assignment, Arithmetic, and Unary Operators (The Java™ Tutorials > Learning the Java Language > Language Basics)
- 12-30-2009, 01:35 AM #3
Member
- Join Date
- Dec 2009
- Posts
- 10
- Rep Power
- 0
Hi, thanks for your reply.
Sorry, sir. I don't get it :(
- 12-30-2009, 04:49 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
I don't get it
Is there something specific in the tutorial section that you don't understand? Or can you not see how to apply the / and % operators to your problem?
Java Code:public class Cents { public static void main(String[] args) { int test = 12345; System.out.println("Suppose I have " + test + " cents"); System.out.print("That would be " + (test / 100) + " dollars "); System.out.print("and " + (test % 100) + " cents"); } }
- 01-01-2010, 01:42 AM #5
Member
- Join Date
- Dec 2009
- Posts
- 10
- Rep Power
- 0
Hi sir. I did not know how to apply the % operator, sorry. It works fine now. Thank you very much for your help.
- 01-01-2010, 02:37 AM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks