0
- 從我的作業中,我需要讓用戶輸入一個數字形式的數字,並在使用遞歸時將其轉換爲序列中的同步斐波那契數。
我的問題是如何使序列通過數組,但不能存儲,所以該陣列可以是用戶輸入的數字的大小...... 下面是一些開始的代碼,我:使用遞歸確定用戶輸入的斐波那契數
import java.util.Scanner; public class ReverseUserInput1 { //a recursive method to reverse the order of user input public static void main(String[] args) { Scanner in = new Scanner(System.in); ReverseUserInput1 reverseIt = new ReverseUserInput1(); //creates new object System.out.print("Program to convert a number to a fibonacci number,"); System.out.print(" - press Enter after each number. "); System.out.println("- type \'0 or 1\' to finish the program."); System.out.print(" --Enter a number: "); int aNum = in.nextInt(); reverseIt.reverseInput(aNum); //invokes reverseInput() method } public static int reverseInput() { if(aNum == 0) { return aNum; } else if(aNum == 1) { return aNum; } else { reverseInput(); } System.out.println(aNum); } }
我認爲你應該爲你的家庭作業做的是創建一個遞歸方法'int fibonacci(int n)',它返回斐波那契數列中的第n個數字。你必須從用戶輸入中讀取參數'n'。請看看斐波那契數列是什麼,查看遞歸的註釋,並給它一個提示。目前,您的reverseInput()不起作用。確定任何斐波納契數是前2的總和,除了前兩個數字是1或者只是Google的遞歸斐波那契數,但是因爲這是你的作業有點便宜。 –
快速谷歌搜索將返回足夠多的點擊量 - 包括在這個自己的網站上的問題 – fge
爲什麼不使用Binet公式?溝渠遞歸在這裏。 :d –