关于java,小新真的不会

请找出以下数列的规律,使用循环语句,计算第15个数字是多少:1,2,3,5,8,13,21,34...

img

脑子不行

按照你的思路大体上应该是
for (int i = 3; i <= a; i++)
{
c = b + c;
b = c - b;
}

建议参考以下完整代码

import java.util.Scanner;
 
public class Test24 {
    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        System.out.println("请斐波那契数列的项数:");
        int num=scanner.nextInt();
        int s=func(num);
        System.out.println("斐波那契数列的第"+num+"项为"+s);
    }
 
    private static int func(int num) {
        if(num==1||num==2){
            return 1;
        }else{
            return func(num-1)+func(num-2);
        }
    }
}

斐波拉切数列



public class Test {
 
    private static int a;
 
    public static int getValue(int index) {
        if (index == 1) {
            a = 1;
        }
        if (index == 2) {
            a = 1;
        }
        if (index >= 3) {
            a = getValue(index - 1) + getValue(index - 2);
        }
        return a;
    }
 
    public static void main(String[] args) {
        System.out.print(getValue(15));
    }
}

鸽们儿,看我看我,我这里花了一些时间给你写了一份,以评论,莫辜负,希望你能够采纳
————————————————————————————————————————
这是一个斐波那契数列,规律是从第三项开始,每一项都等于前两项之和。
以下是用Java实现计算第15个数字的代码:

public class Fibonacci {
    public static void main(String[] args) {
        int n = 15;
        int a = 1, b = 2, c = 0;
        for (int i = 3; i <= n; i++) {
            c = a + b;
            a = b;
            b = c;
        }
        System.out.println("第" + n + "个数字是:" + c);
    }
}

第15个数字是:610

不知道你这个问题是否已经解决, 如果还没有解决的话:

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^