Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

loops

 
John Swain
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello, this is a simple question. could some one tell me how to use a loop so that a program will run untill the user enters "no". This would be the outer loop and other calculations would be done in the inner loop. Could i use boolean for this? if not please explain.
 
Jimmy Die
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,


How I see the problem is that once you start a loop that will keep running until someone presses the letters 'n' and then 'o' and then 'enter' at the command prompt is that the loop will be running in single thread. You will have to develope another thread to listen and/or check for input for each iteration.

Boolean could be used depending upon your setup.

Write what you have so far on the post, and someone can help you along.
 
John Swain
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, I am very new to java so if my code is extremly messed up please dont mind.


this is only partial code. I want to add the outer loop first, which is to get the user to enter a response of yes or no. if yes then the inner loop initiates and does calculations. those are for annual interest, monthly interest, and daily interest within a 10 year period (inner loop).
 
fred rosenberger
lowercase baba
Bartender
Posts: 12196
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
there are two possible interpretations when you say "...a program will run until the user enters 'no'"

1) the program asks for some input. the user enters data. the program calculates a result. the program says "do you want to do it again?", and waits for an input

2) the program starts calculating/doing something. maybe drawing a moving image across a screen, maybe calculating the digits of pi, whatever. it's going great guns-a-blazing, using as much cpu time as it can get. the program never explicitly says "should i stop now?" - it just keeps going, until some event, like the user typing 'no' or hits 'ESC'.

these are two very different problems. I think Jimmy was answering the latter problem. if you're just starting java, I hope you're doing the former.

to do it, you could have a do/while loop. this garantees your loop executes at least once. assuming you can get the user's input, you'd then (in your while condition) compare what they input to some literal, e.g. "NO". as long as the string they input is NOT the same, your loop will continue.

there are a few caveats - you need to use the .equals method, not == for the string comparison. if you compare against "NO", what do you want to do if the user enters "no", "No", "N" or "n" (there are other possibilities to consider here as well).

Hopefully this will give you some ideas what to do next, or what questions you need to ask next.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic