Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: problems with loop

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default problems with loop

    Hi i juste want to know why this loop only executate 1 times instead of infinite(that's what i want)



    while (answer<1||answer>5);{
    System.out.println("Votre choix est invalide");
    System.out.println("Veuillez Choisir 1 des 4 groupes alimentaires suivants: (1)Légumes et fruits , (2) Produit cérealiers , (3) Laits et Substitues , (4) Viandes et substitues :");
    answer = Clavier.lireIntLn();
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,503
    My Mood
    Mellow
    Thanks
    153
    Thanked 610 Times in 597 Posts

    Default Re: problems with loop

    Please read the Announcement topic at the top of each sub-forum to learn how to post your code in code/highlight tags.

    I assume answer = 0 the first time the loop executes. Then what happens the next time the loop condition is evaluated if the user chooses 3?

  3. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: problems with loop

    well its supposed to print invalid answer each time the answer is not 1,2,3 or 4

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,503
    My Mood
    Mellow
    Thanks
    153
    Thanked 610 Times in 597 Posts

    Default Re: problems with loop

    I asked the question poorly or you misunderstood. I'll try again.

    answer = 3
    while (answer<1||answer>5)

    Does the while loop execute?

    Oh crap! (Forgive my French.) Another thing you should do is remove the semi-colon from the end of the while loop statement.

  5. #5
    Junior Member
    Join Date
    Oct 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: problems with loop

    Its executing but incorrectly , its says invalid choice even if its one of the right answer, and then he asks again , then stops

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,503
    My Mood
    Mellow
    Thanks
    153
    Thanked 610 Times in 597 Posts

    Default Re: problems with loop

    I don't know how to point you to the problem any more specifically:

    CHECK THE LOGIC OF THE WHILE CONDITION FOR answer = 1 - 4! (Yes, I'm shouting.)

Similar Threads

  1. problems with for loop
    By sorbazz in forum Loops & Control Statements
    Replies: 7
    Last Post: December 6th, 2012, 06:48 AM
  2. For loop; Problems with my for loop
    By mingleth in forum Loops & Control Statements
    Replies: 5
    Last Post: November 16th, 2011, 06:24 PM
  3. [SOLVED] Problems with Loop Structure
    By Sean137 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: December 13th, 2010, 01:59 PM
  4. for loop and while loop problems
    By Pulse_Irl in forum Loops & Control Statements
    Replies: 4
    Last Post: May 3rd, 2010, 02:09 AM
  5. problems with loop in Java App
    By dmonx in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 13th, 2010, 03:13 PM

Tags for this Thread