# How to loop a question when answer is negatve?

• 04-23-2009, 04:20 AM
brazuca
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.)
• 04-23-2009, 04:27 AM
Fubarable
• 04-23-2009, 04:39 AM
brazuca
I removed it, but it was after

I just tried making it an "if else" statement, but I am not sure if that will cause the question to repeat.
• 04-23-2009, 04:42 AM
Fubarable
Have you studied creating separate methods yet? If you have, then using one can greatly simplify this problem here.
• 04-23-2009, 05:21 AM
brazuca
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?
• 04-23-2009, 05:25 AM
Fubarable
I don't use a Console class, so I'm not sure how it works in particular, but I imagine something like:

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);```
• 04-23-2009, 05:46 AM
brazuca
Thank you so much. I managed to make it work using the code you provided!