Results 1 to 11 of 11
Thread: Coin assigning program
- 03-11-2011, 04:07 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 16
- Rep Power
- 0
Help with coin assigning program
Hi. I'm trying to make a program, with which the user can input a number of pennies and the program will assign each part of the value to the appropriate coin.
For example, if the user inputs 123, the program would output:
Or, if the user inputs 546, the program would output:Java Code:toonies = 0 loonies = 1 quarters = 0 dimes = 2 nickles = 0 pennies = 3
It needs to be able to do that with any int value. Here's what I have so far. I've finished all the user input code and I'm in the middle of trying to make it assign the value to the correct coins, but I think I'm on the wrong track.Java Code:toonies = 2 loonies = 1 quarters = 1 dimes = 2 nickles = 0 pennies = 1
I'm hoping someone can tell me how to make it assign the coins correctly.Java Code:import java.io.*; class partThree { public static void main(String[] args) throws IOException { InputStreamReader inStream = new InputStreamReader (System.in); BufferedReader stdin = new BufferedReader(inStream); String inData; int money = 0; int toonies = 0, loonies = 0, quarters = 0, dimes = 0, nickles = 0, pennies = 0; System.out.println("Enter your amount of money in pennies:"); inData = stdin.readLine(); money = Integer.parseInt(inData); if (money >= 200) { while (money != 0) { toonies = 1; money -= 200; if (money >= 100) {
If you need any additional information, let me know.Last edited by Jamesing; 03-11-2011 at 04:32 AM.
- 03-11-2011, 04:31 AM #2
Use divide and mod.
Java Code:int value = 123; System.out.println(value / 100); System.out.println(value % 100);
- 03-11-2011, 04:38 AM #3
Member
- Join Date
- Feb 2011
- Posts
- 16
- Rep Power
- 0
That helps, but I'm not sure how I would connect it with the coin variables
Java Code:int toonies, loonies, quarters, dimes, nickles, pennies;
- 03-11-2011, 04:47 AM #4
I'm not Canadian so do not know what toonies and loonies are worth. I assume though they are in descending order.
You start by dividing your amount by toonies to see how many toonies there are. then you mod the amount by toonies to get the remainder.
You then divide your amount by loonies etc.
- 03-11-2011, 04:49 AM #5
Maybe if you think about it some more you will. 7 minutes may not be long enough.I'm not sure how I would connect it with the coin variables
dbLast edited by DarrylBurke; 03-11-2011 at 04:52 AM.
- 03-11-2011, 04:52 AM #6
Member
- Join Date
- Feb 2011
- Posts
- 16
- Rep Power
- 0
Okay, thanks for the help. :)
- 03-11-2011, 04:53 AM #7
Member
- Join Date
- Feb 2011
- Posts
- 16
- Rep Power
- 0
- 03-11-2011, 05:27 AM #8
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
...however useless it may be.
We're getting a bit of an outbreak of this.
@OP: This is not an attitude you are going to find helpful.
Rather as db suggests, stop and think about the problem. It's the sort of task primary school children might be given: from some large pile of tokens representing pennies, figure out how much money you have.
The only difference is that we use mathematical words. The children would form heaps of tokens (big toony heaps, smaller loony heaps with the remainder etc). Finding the number of heaps of a given size is called "division". Finding the remainder is called "modulus". And Java has built in operators for both.
- 03-11-2011, 05:32 AM #9
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
I just did this earlier in c++, the way you were doing it will work fine, it's how I did it. Take some time and really think about how to do it. Also take the time to understand how it can be done with modulus instead(%)
Your way will actualy not work easily. You can however; do it with one if else clause per coin type.Last edited by sunde887; 03-11-2011 at 05:35 AM.
- 03-11-2011, 05:53 AM #10
Member
- Join Date
- Feb 2011
- Posts
- 16
- Rep Power
- 0
Yes, I understand. I have already put a lot of thought into the problem. I have considered using the "/" and "%" operators, but I initially wasn't sure how I would use them, so I tried using the "if" statements instead, as you can see in my code, but after I was a little ways through that, I thought about it some more and it didn't seem like it was going to work, so I thought someone on this forum might know how. But I think the issue is solved now. Thanks.
- 03-11-2011, 05:57 AM #11
Member
- Join Date
- Feb 2011
- Posts
- 16
- Rep Power
- 0
Similar Threads
-
Java Coin Acceptor?
By starzsimon in forum Advanced JavaReplies: 5Last Post: 08-06-2011, 07:50 AM -
Coin application
By kkGG in forum New To JavaReplies: 4Last Post: 11-08-2010, 12:24 AM -
Coin Change
By Growler in forum New To JavaReplies: 7Last Post: 09-23-2010, 05:33 PM -
coin toss program(Eclipse)
By ccie007 in forum New To JavaReplies: 5Last Post: 08-06-2010, 07:03 PM -
Random coin flip application
By Boomer1 in forum New To JavaReplies: 8Last Post: 12-18-2009, 02:57 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks