บทความนี้จะอธิบายในรูปแบบของตัวอย่างว่า Java นำลำดับฟีโบนัชชีไปใช้อย่างไรโดยยึดตามจำนวนเต็มที่มีความแม่นยำสูง และแบ่งปันกับคุณเพื่อเป็นข้อมูลอ้างอิง วิธีการเฉพาะมีดังนี้:
package com.java.learning.recursion;import java.math.*;public class MainClass { public static void main(String args[]){ for(int i = 0; i < 100; i++){ f(i+1) } } BigInteger สาธารณะ f(long n){ if(n <= 2){ return new BigInteger("1"); }else{ BigInteger n1 = new BigInteger("1"); BigInteger n2 = new BigInteger("1"); BigInteger temp = new BigInteger("0"); สำหรับ (ยาว i = 0; i < n -2; i++){ temp = n1.add (n2); n1 = n2; n2 = temp; } System.out.println("รายการ " + n + "th คือ: " + n2); -
ฉันหวังว่าบทความนี้จะเป็นประโยชน์กับการเขียนโปรแกรม Java ของทุกคน