我很困惑我爲什麼會遇到錯誤以及如何解決它。我的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;
}
}
}
不知道錯誤是什麼,您如何期待我們解決問題? –