Results 1 to 7 of 7
  1. #1
    brazuca is offline Member
    Join Date
    Apr 2009
    Posts
    4
    Rep Power
    0

    Thumbs down 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. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Where's your while loop?

  3. #3
    brazuca is offline Member
    Join Date
    Apr 2009
    Posts
    4
    Rep Power
    0

    Default

    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. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

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

  5. #5
    brazuca is offline Member
    Join Date
    Apr 2009
    Posts
    4
    Rep Power
    0

    Default

    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. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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. #7
    brazuca is offline Member
    Join Date
    Apr 2009
    Posts
    4
    Rep Power
    0

    Default

    Thank you so much. I managed to make it work using the code you provided!

Similar Threads

  1. Answer To Previous Question
    By herbozo2003 in forum Java Servlet
    Replies: 0
    Last Post: 03-02-2009, 02:22 AM
  2. Repeat while answer yes - do while loop
    By AJ2009 in forum New To Java
    Replies: 1
    Last Post: 01-10-2009, 06:19 PM
  3. Plz answer this question ...
    By raghu2114 in forum Advanced Java
    Replies: 2
    Last Post: 09-19-2008, 06:36 PM
  4. Replies: 1
    Last Post: 08-29-2008, 01:54 PM
  5. Question regarding foreach loop...
    By theonlywalks in forum New To Java
    Replies: 2
    Last Post: 03-15-2008, 06:15 AM

Posting Permissions

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