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);
    }
}
VernonDozier commented: Code tags on first post. +27

Recommended Answers

All 4 Replies

Your program doesn't compile. Look at your while loops. Here's a template:

for (/* variable initialization*/; /* check condition */; /* variable adjustment */)
{
  // code to repeat
}


/* variable initialization */
while (/* check condition */)
{
   // code to repeat.
   /* variable adjustment */
}

Your while loops look like for-loops. Change them to something similar to the template above.

ok i fixed my loops but now i am thinking i would like to take the function out of the end of my program and instead use 3 variables instead (int variables)
but no matter wut i try it just doesnt seem to work out for me

import javax.swing.JOptionPane;

public class ex2
{
  public static void main(String[] args)
  {
    for1();
    while1();
  }
  public static void for1()
  {
    int numFibs = (Integer.parseInt(JOptionPane.showInputDialog("Enter the number of Fibonnacci Numbers to Display")));

for(int i = 1; i <= numFibs; i++)
{
   System.out.println(fib(i));
}
  }
  public static void while1()
  {
     int numFibz = (Integer.parseInt(JOptionPane.showInputDialog("Enter the number of Fibonnacci Numbers to Display")));
     int i = 1;
     while(i <= numFibz)
     {
       System.out.println(fib(i));
       i++;
     }
  }
  public static int fib(int n)
    { 
       if (n <= 2)
            return 1;
       else
            return fib(n - 1) + fib(n - 2);
    }
}

What's the problem? Your program runs fine for me. You want a non-recursive solution is that it?

int maxNumIterations = 30;
int fib[] = new int[maxNumIterations];

fib[1] = 1;
fib[2] = 1;

// set up a loop with n as the loop control variable.  fib[n] = fib[n-2] + fib[n-1];

I'm not sure when Fibonacci numbers begin to overflow. It'll be above iteration 30 for sure. I imagine they'll get to around a billion somewhere around iteration 50 and will overflow soon afterward.

lol fibonacci will eventually eat the compiler if u leave out a parameter
i know i did it by accident
i think i forgot to set the max value i wanted to be displayed and it kept going and going and going.......eventually it took a whole minute to get from one value to the next lol

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.