Posted by

jdtan on November 16, 2009 at 6:00 AM PST

Im in desperate need of help. I need to modify this code which is doing Fibonacci Numbers. I need to modify the code so that it restricts the value of the first number to a whole number greater than or equal to zero, restrict the value of the second number greater than or equal the first number, and restrict the value of the input when prompted for the element to dsiplay a whole number greater than or equal to one.

I'm really confused and need help

This is a part of the code i need to modify.

String inputString;

String outputString;

int previous1;

int previous2;

int current = 0;

int counter;

int nthFibonacci;

inputString = JOptionPane.showInputDialog("Enter the first " + "Fibonacci number; ");

previous1 = Integer.parseInt(inputString);

inputString = JOptionPane.showInputDialog("Enter the second " + "Fibonacci number; ");

previous2 = Integer.parseInt(inputString);

outputString = "The first two number sof the " + "Fibonacci sequence are: " + previous1 + " and " + previous2;

inputString = JOptionPane.showInputDialog("Enter the position " + "of the desired number in " + "the Fibonacci sequence: ");

nthFibonacci = Integer.parseInt (inputString);

if (nthFibonacci == 1)

current = previous1;

else if (

else if (nthFibonacci == 2)

current = previous2;

else

{

counter = 3;

while (counter <= nthFibonacci)

{

current = previous2 + previous1;

previous1 = previous2;

previous2 = current;

counter++;

}

}

outputString = outputString + "\nThe " + nthFibonacci + "th Fibonacci number of " + "the sequence is: " + current;

JOptionPane.showMessageDialog(null, outputString, "Fibonacci Number", JOptionPane.INFORMATION_MESSAGE);

System.exit(0);