# Thread: help needed plz with some loop problem I guess

1. Member
Join Date
Jul 2011
Posts
5
Rep Power
0

## help needed plz with some loop problem I guess

Hi, I have a problem.
I have to solve a problem, but I cant seem to work it out.
I have to give change to some amount.
I tried some loop things but I cant seem to make it work.
the problem is that I have to give back "money"( only in values of 50, 20 and 10) to any amount.
for example if someone types in 90 I have to give back 1x50, 2x20. if some one type in the number 96 the machine rounds up to 100 and have to give back 2x50. if someone types in 130 it will be 2x50, 1x20 and 1x10 so I always have to give back the highest possible value (50 , 20 or 10)
Can someone help me plz!!!

thnx

T

2. Please see the link in my signature on asking questions the smart way before posting again.

You haven't provided any information that we can use to help you- create an SSCCE (that does NOT mean paste in your entire program) that demonstrates what you're talking about, be specific, and we'll go from there. You saying "I tried some loop things but I can't seem to make it work" is as useful to us as us saying "then try some other loop things that do work" is useful to you.

3. Member
Join Date
Jul 2011
Posts
5
Rep Power
0
I'm sorry.
this is what I tried already

public class Money {

public static void main (String args[]) {

int inputAmnt = Integer.parseInt(args[0]);
int amnt50 = 50;
int amnt20 = 20;
int amnt10 = 10;
int rest;

System.out.println("Input amount = " + inputAmnt);
while(amnt50 <= inputAmnt){
int rest1 = inputAmnt - amnt50;
System.out.println(" u krijgt 1 x 50 ");
while (amnt20 <= rest1){
int rest2 = rest1 - amnt20;
System.out.println(" u krijgt 1 x 20 ");
while (amnt10 <= rest2){
int rest3 = rest2 - amnt10;
System.out.println(" u krijgt 1 x 10 ");
}
}
}
}
}

4. You forgot the code tags.

And what does that code do? What did you expect it to do? What happened when you stepped through it with a debugger?

5. Member
Join Date
Jul 2011
Posts
5
Rep Power
0
well...this discribes my problem quit well no? :s
I'm using notepad++ by the way.
I don't know how tho explaine it otherwise.
I have the mathematical problem that I have to solve. but that is easy, but I don't know how to write it in code...
I tried this because it calculates how for example how many times 50goes into 90, that is one, and then you get 40 and then have to calculate how many times 20 goes into 40, 2 time, but then the code gets into an infinite loop there.

6. then the code gets into an infinite loop
What controls the looping? Is there some variable's value that keeps the code in the loop? Can you change that value so that the loop will exit?
If you don't know how the values are changing in the loop, add a println to print out the values of ALL the variables used in that loop. Looking at the output should show you where they are not being changed as they need be.

7. Member
Join Date
Jul 2011
Posts
5
Rep Power
0
Thanks for the fast replies, but I finally got it!! yes:p
the while loops where nested in each other apparently :s
when I sepperated them it worked!! now I have to trie to write classes for each note...back to work :)
it's fun, but I find it very difficult to see all this.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•