Just beginning to learn java so this has had me stuck for quite some time now. This is a program to output numbers of the Fibonacci sequence. The goal here is to get the program to output only the last line of the while loop. I've got the program up and running, just having trouble trying to figure out how to only output the last line of the sequence
Example Input/Output Input: 5
Output: Fibonacci #3 is: 3 Fibonacci #4 is: 5 Fibonacci #5 is: 8
Output should look like this: Fibonacci #5 is: 8
import javax.swing.JOptionPane;
public class Fibonacci2 {
public static void main(String args[ ]) {
int n,
counter = 2,
sum,
prevN,
nextN;
String inputValue;
inputValue = JOptionPane.showInputDialog(null, "Enter Number Greater than 2: ",
JOptionPane.QUESTION_MESSAGE);
n = Integer.parseInt(inputValue);
if (n >= 2)
counter = 2;
prevN = 1;
nextN = 2;
if (n < 2)
System.out.println("Invalid Input. Please Try Again.");
while (counter < n)
{
sum = prevN + nextN;
prevN = nextN;
nextN = sum;
counter++;
System.out.println("Fibonacci #"+counter+" is: " + sum);
}
System.exit(0);
}
}