# Thread: How to loop a question when answer is negatve?

1. Member
Join Date
Apr 2009
Posts
4
Rep Power
0

## How to loop a question when answer is negatve?

Hey guys I am trying to make this program that checks what your final grade is. I managed to make it work pretty well for someone new to java.

When you run the program your suppose to put the grade you got for the tests and stuff and it will then calculate your grade.

Now I am trying to make it so if a person puts a negative answer then it will ask the question again. I tried making a do while loop, but for some reason it keeps looping the first question non stop.

Can anyone help me make the program loop a question if the answer is negative or isn't a positive double? (a1, a2, t1, etc.)
Last edited by brazuca; 04-23-2009 at 06:31 AM.

2. Where's your while loop?

3. Member
Join Date
Apr 2009
Posts
4
Rep Power
0
I removed it, but it was after

a1 = Console.readDouble ("\nEnter the grade for your 1st assignment: ");

I just tried making it an "if else" statement, but I am not sure if that will cause the question to repeat.
Last edited by brazuca; 04-23-2009 at 04:42 AM.

4. Have you studied creating separate methods yet? If you have, then using one can greatly simplify this problem here.

5. Member
Join Date
Apr 2009
Posts
4
Rep Power
0
I just started studying coding so I don't know much more than what I have written. Other than that I made a program that makes shapes with stars.

How can I make it check after person puts an answer for (a1 <= 0) that it will repeat the question otherwise it would go to the next question which is a2?

6. I don't use a Console class, so I'm not sure how it works in particular, but I imagine something like:

Java Code:
```do
{
a1 = Console.readDouble ("\nEnter the grade for your 1st assignment: ");
if (a1 < 0)
{
System.out.println("number entered must be positive only.")
}
} while (a1 < 0);```

7. Member
Join Date
Apr 2009
Posts
4
Rep Power
0
Thank you so much. I managed to make it work using the code you provided!

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•