Java Reference
In-Depth Information
outputString = "The first two numbers of the "
+ "Fibonacci sequence are: "
+ previous1 + " and " + previous2;
//Step 5
inputString =
JOptionPane.showInputDialog("Enter the position "
+ "of the desired number in "
+ "the Fibonacci sequence: ");
//Step 6
nthFibonacci = Integer.parseInt(inputString);
//Step 7
if (nthFibonacci == 1)
//Step 8.a
current = previous1;
else if (nthFibonacci == 2)
//Step 8.b
current = previous2;
5
else
//Step 8.c
{
counter = 3;
//Step 8.c.1
//Steps 8.c.2 - 8.c.5
while (counter <= nthFibonacci)
{
current = previous2 + previous1;
//Step 8.c.2
previous1 = previous2;
//Step 8.c.3
previous2 = current;
//Step 8.c.4
counter++;
//Step 8.c.5
}
}
outputString ΒΌ outputString + "\nThe "
+ nthFibonacci
+ "th Fibonacci number of "
+ "the sequence is: "
+ current;
//Step 9
JOptionPane.showMessageDialog( null , outputString,
"Fibonacci Number",
JOptionPane.INFORMATION_MESSAGE);
//Step 10
System.exit(0);
}
}
Search WWH ::




Custom Search