• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need help with one of my first scripts.  RSS feed

 
Macaurly Windle
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I have written the following code which works fine. I have the condition of the user input number being less than 10. I would like to know how to make the code run itself again and ask for another input if the first input is greater than 10. So if the user were to enter a number greater than 10 2 times, the program would automatically prompt for another two inputs before running properly. I have a feeling I would need to make a method which contains the switch method and call it on the user input variable called 'number'. The extent of my java knowledge is that of the codeacademy interactive lessons (free version).
Also, I apologise if I am not following a convention by posting all my code in the text box here. If there is a better way to do this please let me know
Thanks.


 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37462
537
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Macaurly,
Welcome to CodeRanch! You posted your code correctly.

Have you learned about while loops yet?
 
Macaurly Windle
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jeanne,
I do know how to use them yes.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37462
537
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'll need them here to solve your problem. The method idea is good for readability - but the loop is needed to prompt the user multiple times.

For the while condition think about what value of number is invalid and will therefore require the loop to keep going.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again
I presume that is an exercise in using a switch statement. It might be easier with an array of Strings otherwise.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!