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

Thread: need help with loops plz

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

    Default need help with loops plz

    hi i am new to java programming and im working on a looping program involving the fibonacci numbers.
    what i would like to do is to have a program that asks the user how many values of the Fibonacci sequence to output. Then i want to create a loop that outputs the number of values of the Fibonacci sequence requested by the user.
    i only want to use the do....while loop, for loop, and the while loop
    the following code i will post does not do what i want and i cannot figure out why
    first off when i input a number it doesnt display the desired out......it just displays the same exact 15 numbers
    and second my for loop works but all the other ones do not
    any help would be very much appreciated ty

    import javax.swing.JOptionPane;
     
    public class Lab4
    {
      public static void main(String[] args)
      {
        String aString="15";
        String userInput = JOptionPane.showInputDialog("How many values of the Fibonacci sequence would you like?");
        int aInt = Integer.parseInt(aString);
        for1();
        do1();
        while1();
        for2();
        do2();
        while2();
      }
     public static void for1()
     {
        String aString="15";
        String userInput = JOptionPane.showInputDialog("How many values of the Fibonacci sequence would you like?");
        int aInt = Integer.parseInt(aString);
        for(int i = 1; i <= aInt; i++)
        {
          int f = fib(i);
          System.out.println("Fib(" + i + ") = " + f);
        }
      }
     
     
     public static void do1()
     {
        String bString="15";
        String userInput = JOptionPane.showInputDialog("How many values of the Fibonacci sequence would you like?");
        int bInt = Integer.parseInt(bString);
       do
       {
         int f = fib(i);
       }
       while(int h = 1; h <= bInt; h++);
       System.out.println("Fib(" + h + ") = " + f);
     }
     
     public static void while1()
     {
        String cString="15";
        String userInput = JOptionPane.showInputDialog("How many values of the Fibonacci sequence would you like?");
        int cInt = Integer.parseInt(cString);
        while(int i = 1; i <= cInt; i++)
        {
          int f = fib (i);
          System.out.println("Fib(" + i + ") = " + f);
        }
     
     
      public static int fib(int n)
        { 
           if (n <= 2)
                return 1;
           else
                return fib(n - 1) + fib(n - 2);
        }
    }
    Last edited by JavaPF; September 23rd, 2009 at 03:16 PM. Reason: Please use [code] ... [/code] tags


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: need help with loops plz

    public static void main(String[] args)
    {
         Scanner input = new Scanner(System.in);
         System.out.println("Input number of fib numbers to finds: ");
         int end = input.nextInt();
         System.out.println("1\n1");
         int fib1 = 1;
         int fib2 = 1;
         for(int i = 3; i < end; i++)
         {
              int temp = fib1+fib2;
              System.out.println(temp);
              fib1 = fib2;
              fib2 = temp;
         }
    }

  3. #3
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: need help with loops plz

    i've red a book exercise and it describes fibonacci , ( a pare of rabbit that matures in 2 months and capabale of reproducing every after maturity), is that exactly what fibonacci system is?

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: need help with loops plz

    mm... rabbits don't normally reproduce via the fibonacci sequence. Instead, they reproduce exponentially (actually, to be more precise the produce logarithmically, at some point their population will be limited by some factors). Neither of these follows the fibonacci sequence.

    The fibonacci sequence is this:

    Start with 1 and 1.

    Add the terms: 1+1 = 2 is the next term.

    Keep adding the 2 previous terms for the next term.

    1+2=3
    2+3=5
    3+5=8
    ....

    In fact, the fibonacci grows at a much faster rate than most functions (even exponentials). I think the one exponential that does grow faster than fibonacci is x^x (don't take my word for it, though). I'm not sure if fibonacci grows faster than factorials, either.
    Last edited by helloworld922; September 23rd, 2009 at 06:40 PM.

  5. #5
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: need help with loops plz

    i didnt post the exact details.. here it is..


    "suppose we have a pair of rabbits (assuming that these rabbits dont die) a pair of rabbits will mature in 2 months. and they are capable of reproducing another pair after maturity(after 2 months)

    is this a fibonacci sequence?

Similar Threads

  1. Homework - using 'IF' for 'For Loops'
    By Enzo in forum Loops & Control Statements
    Replies: 5
    Last Post: September 16th, 2009, 10:52 AM
  2. How to Use different Java loops
    By JavaPF in forum Java Programming Tutorials
    Replies: 1
    Last Post: August 28th, 2009, 03:10 AM
  3. Breaking out of multiple loops
    By helloworld922 in forum Loops & Control Statements
    Replies: 4
    Last Post: July 1st, 2009, 02:52 PM
  4. Replies: 11
    Last Post: April 29th, 2009, 03:12 AM
  5. [SOLVED] Fixing of bug in number guessing game
    By big_c in forum Loops & Control Statements
    Replies: 6
    Last Post: April 16th, 2009, 02:15 AM