我很困惑我为什么会遇到错误以及如何解决它。我的fibonacci序列版本应该只打印所需的目标索引值,并不是所有数字都像我见过的大多数其他斐波那契数列一样。在Java斐波那契数列中编译错误
import java.util.Scanner;
public class Fibonacci_ronhoward
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("This is a Fibonacci sequence generator");
System.out.println("Choose what you would like to do");
System.out.println("1. Find the nth Fibonacci number");
System.out.println("2. Find the smallest Fibonacci number that exceeds user given value");
System.out.println("Enter your choice: ");
int choice = scan.nextInt();
switch (choice)
{
case 1:
System.out.println();
System.out.println("Enter the target index to generate (>1): ");
int n = scan.nextInt();
int a = 0;
int b = 1;
for (int i = 1; i < n; i++)
{
int nextNumber = a + b;
a = b;
b = nextNumber;
}
System.out.println("The " + n + "th Fibonacci number is " + nextNumber + " ");
break;
}
}
}
不知道错误是什么,您如何期待我们解决问题? –