Page 1 of 1

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

Posted: Wed Nov 26, 2003 5:59 pm
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?

Posted: Wed Nov 26, 2003 6:34 pm
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]

Posted: Wed Nov 26, 2003 7:09 pm
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
}
}

Posted: Wed Nov 26, 2003 7:10 pm
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();
}

Posted: Wed Nov 26, 2003 9:19 pm
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.