• Post Reply Bookmark Topic Watch Topic
  • New Topic

loop what am I doing wrong?  RSS feed

 
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys I just wrote a small quick program for a water system so everything compiles and is good so far BUT I'm having trouble figuring out what loop to use,I basically want the user to choose if he wants to add more water and use this as a loop until he wants to stop adding water,but I wonder what I could do to implement this.Thanks heres my code





Thanks =) my problem is between 23 and 32 on the second strip of code
 
Saloon Keeper
Posts: 3336
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have two prompts, lines 22 and 27, why not just eliminate line 22 and bail out (break) after a user enters zero for the prompt on line 27.
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:You have two prompts, lines 22 and 27, why not just eliminate line 22 and bail out (break) after a user enters zero for the prompt on line 27.


nice eye man,worked great the break statement even though was focusing on it few days ago didn't even cross my mind lol

Thanks =)
 
Sheriff
Posts: 4935
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam Chalkley wrote:
Carey Brown wrote:You have two prompts, lines 22 and 27, why not just eliminate line 22 and bail out (break) after a user enters zero for the prompt on line 27.


nice eye man,worked great the break statement even though was focusing on it few days ago didn't even cross my mind lol

Thanks =)

And that zero value calls "sentinel value"
 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you are doing is setting a variable to continue the loop (you can't write “continue” because that is a keyword). You are then starting the loop with == 1, which is perfectly all right. But then you are never changing the value of cont. You are changing the value of amount. So rather than using break; you have an alternative. Well, several alternatives. Here are two.
  • 1: Repeat the loop as long as amount is not 0. You may find a do loop works well for that.
  • 2: Ask for cont to be entered at the end of the loop, maybe like this: System.out.print("Enter 1 to repeat: "); cont = myScanner.nextInt();
  •  
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!