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 4 of 4

Thread: Beginner's Looping Issue

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

    Default Beginner's Looping Issue

    Hello,

    My goal is to produce a menu of a series of options. Options 1 & 2 will perform a function, and then loop back to the menu, 3 will exit the menu, and 4 will print "wrong option" for selections other than 1-3. I can't seem to get the options to look back to the menu once they are completed.

    Here is an "essentials only" version, as the original is pretty long, but it can be provided if necessary.

    public static void main(String[] args) {
    // TODO code application logic here
    int option;
    Scanner input;
    input = new Scanner(System.in);

    System.out.println("enter an option");
    option = input.nextInt();

    if (option != 3)
    {
    if (option == 1)
    {
    System.out.println("option 1");

    }else if (option == 2)
    {
    System.out.println("option 2");
    ;
    }else if (option != 1 && (option != 2))
    {
    System.out.println("wrong option");

    }

    }

    if (option == 3)
    {
    System.out.println("done");
    }
    option = input.nextInt();
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,434
    Thanks
    49
    Thanked 2,216 Times in 2,189 Posts

    Default Re: Beginner's Looping Issue

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    get the options to look back to the menu
    Wrap that part of the code in a loop. When an option ends processing the execution should go back to the top of the loop.
    while or do{}while loops are often used here.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: Beginner's Looping Issue

    Hello.
    You made your logic complex. Lets say I am checking for a condition 'c1' to be true, this is enough.
      if(c1 == true)  {
        // do something
      }
      else  {
        // do something
      }

    But you are trying something like this,
      if(c1 == true)  {
        // do something
      }
      else if(c1 == false)  {
        // do something}
      }

    Definitely if c1 is false in if statement you would ultimately come to else block. In else block there is no need to check for c1 being false.

    Still, your program is correct. You need to put your entire menu code in a loop. Thats it.

    Syed.

  4. #4
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    590
    My Mood
    Sleepy
    Thanks
    10
    Thanked 87 Times in 80 Posts

    Default Re: Beginner's Looping Issue

    And while you're at it. Scratch the comparison in total when testing booleans:
     if(c1)  {
        // do something
      }
      else  {
        // do something
      }

Similar Threads

  1. [SOLVED] Image looping issue
    By Montario in forum What's Wrong With My Code?
    Replies: 35
    Last Post: June 3rd, 2012, 02:54 PM
  2. Need Help on Looping Program!! Beginner!!
    By PinkFly in forum What's Wrong With My Code?
    Replies: 8
    Last Post: March 3rd, 2012, 11:05 PM
  3. For-looping, if-else statements, charAt(), etc. Beginner programming problem
    By ayelleeeecks in forum Loops & Control Statements
    Replies: 11
    Last Post: October 3rd, 2011, 12:54 PM
  4. Re: Help with a looping issue for homework
    By miss doudy in forum Loops & Control Statements
    Replies: 2
    Last Post: December 14th, 2010, 03:07 PM
  5. Help with a looping issue for homework
    By constancez in forum Loops & Control Statements
    Replies: 5
    Last Post: September 20th, 2010, 07:32 AM

Tags for this Thread