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

Threaded View

  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


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