trying to get the hang of loops!!!!!!

Using the Java SDK with TextPad

Moderators: AmigoJack, bbadmin, helios, Bob Hansen, MudGuard

Post Reply
mon.king
Posts: 4
Joined: Thu Nov 20, 2003 1:22 pm

trying to get the hang of loops!!!!!!

Post by mon.king »

i'm trying to get a program to loop so that the correct information is input. the program asks for a xam result between 0 and 10 but if the user types a number larger than 10 the program should ask again for a number between 0 and 10. i have so far only managed to get this to happen once and if the wrong number is input again nothing happens. is some one able to help?
User avatar
Bob Hansen
Posts: 1516
Joined: Sun Mar 02, 2003 8:15 pm
Location: Salem, NH
Contact:

Post by Bob Hansen »

This almost sounds like you want someone to do your homework :?:

Skip the following section and see Java coding subsequently submitted by talleyrand and MudGuard. This was submitted in error as generic vs. Java. :oops: (Dummy ME!).

Generic approach independent of language. Using line numbers for reference clarity here. Use [Section] for code sections

[Start]
1. Set High_variable = 10
2. Set Low_variable = 0

[Input]
1. Set Score_variable = 0
2. Prompt for input between Low_variable and Low_variable
3. Store the input as Score_variable
4. Go to TestScore

[TestScore]
1. If Score_variable < Low_variable goto Low
2. If Score_variable > High_variable goto High
3. Make message string = Score_variable is a valid score. Thank you.
4. Goto OK

[Low]
1. Make message string = You entered Score_variable which is too low. Must be between Low_variable and High_variable.
2. Goto Error

[High]
1. Make message string = You entered Score_variable which is too high. Must be between Low_variable and High_variable.
2. Goto Error

[Error]
1. Display Message string
2. Wait for OK on Message Display
3. Goto Input

[OK]
1. Display Message string
2. Wait for OK on Message Display
3. Goto Continue

[Continue]
.....
.....
.....

[End]
Last edited by Bob Hansen on Wed Nov 26, 2003 9:31 pm, edited 2 times in total.
Hope this was helpful.............good luck,
Bob
User avatar
talleyrand
Posts: 624
Joined: Mon Jul 21, 2003 6:56 pm
Location: Kansas City, MO, USA
Contact:

Post by talleyrand »

The following is rushed but ought to get you in the ballpark

Code: Select all

class Test
{
public static void main(String argv[])
{
   //Start with a known bad
   int input = -1;
   //loop until they provide an acceptable input
   // 
   while (input < -1 || input > 11)
   {
      System.out.println("Please input a number between 1 and 10");
      //This may or may not be exactly right - specifically, it may require a cast
      //It's been too long and I'm too lazy to verify
      input = System.in.readline();
   }

   //do more stuff here
}
}
I choose to fight with a sack of angry cats.
User avatar
MudGuard
Posts: 1295
Joined: Sun Mar 02, 2003 10:15 pm
Location: Munich, Germany
Contact:

Post by MudGuard »

Bob, please...

goto is the worst thing ever invented....

Pseudo code below:

input = -1; //use a value here which is outside the allowed range
while ((input < 0) or (input > 10)) //loop while input is out of range
{
askuserforinputbetween0and10();
input = getinputfromuser();
}
User avatar
Bob Hansen
Posts: 1516
Joined: Sun Mar 02, 2003 8:15 pm
Location: Salem, NH
Contact:

Post by Bob Hansen »

goto is the worst thing ever invented....
Sorry MudGuard. At the time I replied, I thought this was in the general section, I didn't even realize I was in the Java forum.:oops:

I was trying to use generic non tech terms for what I thought was a new programmer, no language was specified. (Java, dummy!). I guess my spaghetti-code roots are showing. Tried to delete the posting but I guess I can't do that once a following posting has happened. I have modified the original to note its generic purpose.
Hope this was helpful.............good luck,
Bob
Post Reply