創建一個程序,該程序可以輸入一個數字並提供相應的斐波那契數作爲輸出。但是我的代碼出了問題:c斐波納契java程序
import java.util.Scanner;
public class Fibonacci {
public static void main(String[] args) {
Scanner in = new Scanner (System.in);
int prev, next, num, sum = 0, n;
prev=next=1;
System.out.print("Input number: ");
num = in.nextInt();
if ((num == 1) || (num ==2))
System.out.println(prev);
else {
for(n=1; n<=prev; n++) {
sum = prev + next;
prev = next;
next = sum;
}
System.out.println(sum);
}
}
}
有點問題我只是看不到。幫幫我? :c
「錯」不清楚,請解釋發生了什麼問題 – 2014-02-21 19:44:26
「其等價斐波那契數」是什麼意思? –
沒有循環 –