trying to get the hang of loops!!!!!!
Moderators: AmigoJack, bbadmin, helios, Bob Hansen, MudGuard
trying to get the hang of loops!!!!!!
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?
- Bob Hansen
- Posts: 1516
- Joined: Sun Mar 02, 2003 8:15 pm
- Location: Salem, NH
- Contact:
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. (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]
Skip the following section and see Java coding subsequently submitted by talleyrand and MudGuard. This was submitted in error as generic vs. Java. (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
Bob
- talleyrand
- Posts: 624
- Joined: Mon Jul 21, 2003 6:56 pm
- Location: Kansas City, MO, USA
- Contact:
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.
- Bob Hansen
- Posts: 1516
- Joined: Sun Mar 02, 2003 8:15 pm
- Location: Salem, NH
- Contact:
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.goto is the worst thing ever invented....
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
Bob