• Post Reply Bookmark Topic Watch Topic
  • New Topic

From If Loop need to get out if summation reaches 100  RSS feed

 
Md Subrun Jamil
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



==============================================================================


I am a newbie to Java please help me.
 
salvin francis
Bartender
Posts: 1649
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Subrun, Welcome to code ranch, I have modified your post to use code tags to make your program easier to read.
 
salvin francis
Bartender
Posts: 1649
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A couple of things I would like to point out, the "if" in code isnt a loop, its a condition, a statement. I think you need to understand the while loop here.
I understand that you are a bit new to coding. You need to use OOPs in your code and not code everything in the main method. Next, most of the code is actually repeated and I didnt quite understand your required output to help you with the same.
 
Md Subrun Jamil
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi salvin francis

Thanks for your reply. Here in this problem my target is to take user input of any combinations of 25, 10 , 5. Program will receive a combination of these three numbers until it reaches to 100.

So here what I tried to do is , if user is giving input with any of the 3 numbers mentioned earlier I am adding them. Here You will see I added the values 20 times thinking if user is giving input of 5 all times it needs
take input 20 times. But if user gives input with 2 other higher values. It will not be necessary to take input 20 times.  So in this case if I give input with say like 25 & 10 it reaches 100 earlier than if I only give inout with 5 it reaches 100 more earlier than input 5 , but at moment it is not getting out of WHILE Loop.

Hope I am able to make the situation clear.
 
Knute Snortum
Sheriff
Posts: 4274
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you see code like this:

...it means you need a loop.  A do loop or a while loop will work.  Then to break out of the inner while loop, use the keyword break.  Give it a try, post your code, and if you need more help, tell us.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should also think about breaking your code up into many small, simple methods.  In addition to Knute's advice about that code being in a loop, I'd also suggest it go in a method that gets called from inside your loop. 

you should probably spend some time with pencil, paper, and eraser writing down things your program should do - but write in English (or any natural language), not Java. You should be able to explain what your program does in simple steps that a child can understand before you write a single line of code.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can the moneyrcvd1 variable ever get to 100 when you keep changing its value back to whatever the user entered on line 18?  From lines 23 onward, you have multiple statements that add the user's input to moneyrcvd1 but on line 18, all that is superseded by the value that the user enters at that point. You need another variable to track the total that has been accumulated so far. Your while loop should be predicated on that total variable, not the moneyrcvd1 variable.
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Around 80% of that code is unnecessary. You need a way simpler approach.

You have been given a task to collect a certain amount of money, lets say 100 dollars.

What you might need to do:

That word 'valid' on line 3 is important. It supposed to become a method on its own.
 
Md Subrun Jamil
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So  what I did here I added break statement inside while loop though I understand its not efficient code. I will try to improve while adding method.  Please give suggestion how to add method. I have less idea. Yes Definitely I will look at text books for how to use method.

At below code this time I tested the code while giving input with 25 but when I will be giving combination of Coins ( 5 , 10, 25 )  i might need more Inter Coin Statements but how to optimize that rather than not writing huge lines for this purpose ?


 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No improvements. Might even worse. Sorry.

Why are you repeating all that code from line 22 till line 56?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One of hte hardest things to learn when programming is that you need to think more, and type less.  You shouldn't write a line of code until you have a really good idea how things should work. 

Liutauras gave you a really good snapshot of how your code should work. The problem with what you wrote is that it is not scalable...what if the requirement was to go until you had $1000?  or $1,000,000?

What you need is a way to do the same thing over and over, until you meet a condition.  The simplest way to break it down is:



So that looks pretty simple.  the firs line would be something like



Persoanlly, I'd write that loop. Then I'd code it's body with some dummy, throwaway code, like this:



Now I can run this, and see my loop exit when I get enough money. Or I can see i have a syntax error in these five lines, and fix it now.  Then I can recompile and test again.

Once i'm sure that works, i can add a prompt:



compile, debug, test, debut...repeat.

THEN i can add actually getting input from the user




 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are using brute force to repeat doing things with all those if-statements; you don't really need to do that.  That's why you use a loop in the first place, so you can eliminate having to repeat the same thing in the code over and over. 

This is essentially what you're doing:

All those if/break statements are unnecessary! All you have to do is this:

A while-loop is very similar to the above for-loop so you just have to see what can be eliminated and left to the loop to handle and repeat.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!