Results 1 to 5 of 5
  1. #1
    soccer_kid_6 is offline Member
    Join Date
    Feb 2010
    Posts
    35
    Rep Power
    0

    Default Nested Loops for Java

    This is a school assignment

    Ask the user to enter a number. Print "Nested loops are fun." as many times as the user enters a number. (For example, if the user enters the number 3, write "Nested loops are fun." three times. Ask the user if he wants to have some more fun. If he clicks the YES button, do it all over again. If he clicks the NO button, then say "Bye".

    I am having problems connecting the input the user entered to make it loop. My other problem is i dont know what code to enter to make it that when i click no, it will say bye in a dialog box. Below is an attempt of my work. // DO SOMETHING between // DO SOMETHING is my attempt to make it work but i know it is incorrect


    Java Code:
    int number;
    String s;
    int option;
    do
    {
    s = JOptionPane.showInputDialog(null, "Enter a number: ");
    number = Integer.parseInt(s);
    // DO SOMETHING
    do
    {
    if (number <= number) System.out.println("Nested loops are fun.");
    number = number + 1;
    }while (number < number); // end do while loop
    
    //DO SOMETHING
    option = JOptionPane.showConfirmDialog(null, "Want to enter another number?");
    } while(option == JOptionPane.YES_OPTION);
    Last edited by Fubarable; 02-21-2010 at 04:54 AM. Reason: code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Hello, and welcome to the forum!

    First and foremost, let me advise you that when asking a question here, your goal is to make it as easy as possible for others to help you -- that way more volunteers will be willing to do this. So to that end, when posting it's always best to use paste code that has been properly indented and to use code tags to allow the forum to show your formatted code. I've added code tags to your first post, but have not indented it. To learn how to use code tags yourself, please see my signature below.

    Now as to your questions:
    Do both of your loops have to be the same type? You have two different situations here for when the inner loops is called, you'll know at that time how many times it needs to loop -- in fact it loops "number" times. In this situation the best loop to use is a for loop going from int i = 0 to i < number. The outer loop is different for you won't know in advance how many times it should loop and so your do-while loop is just fine.

    Finally as to the bye in a dialog, well you already know how to use JOptionPanes; you'll just want to use a different JOptionPane, a showMessageDialog to be exact.

    Hope this helps and best of luck!

  3. #3
    soccer_kid_6 is offline Member
    Join Date
    Feb 2010
    Posts
    35
    Rep Power
    0

    Default

    Thanks for editing my post I did not notice the place to enter code. Also another thanks for helping me get the inner loop working. The bye dialog is still not working. Is it possible you could me more precise as of what I have to do to make it work?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by soccer_kid_6 View Post
    Thanks for editing my post I did not notice the place to enter code. Also another thanks for helping me get the inner loop working.
    You're quite welcome!

    The bye dialog is still not working. Is it possible you could me more precise as of what I have to do to make it work?
    You first. What are you currently trying, and what does "not working" mean precisely?

    Best of luck!

  5. #5
    soccer_kid_6 is offline Member
    Join Date
    Feb 2010
    Posts
    35
    Rep Power
    0

    Default

    I just made it work I just had to use a if statement. I don't know how this forum works but my question has been resolved if you wish to lock it go ahead and again big thanks to Fubarable

Similar Threads

  1. Itext with java : PDF nested table issues.. Please help
    By akbjavauser in forum Advanced Java
    Replies: 1
    Last Post: 07-14-2010, 05:43 PM
  2. nested for loops
    By Implode in forum New To Java
    Replies: 4
    Last Post: 09-01-2009, 08:47 AM
  3. Nested Loops
    By ks1615 in forum New To Java
    Replies: 4
    Last Post: 02-18-2009, 02:48 AM
  4. JAVA: String char removal with nested loop
    By igniteflow in forum New To Java
    Replies: 3
    Last Post: 11-28-2008, 02:09 AM
  5. Nested loops?
    By gabriel in forum New To Java
    Replies: 4
    Last Post: 08-06-2007, 04:51 PM

Posting Permissions

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