Java 两种不同的输出

Java 两种不同的输出,java,Java,斐波那契序列中的每个新项都是通过将前两项相加生成的。从和开始,前10个术语将是: 1,1,2,3,5,8,13,21,34,... 这里我们应该找到斐波那契数列中的偶数,然后把它们加到和中 以及守则: import java.util.*; public class Abhi { public static void main(String[] args) { Scanner in = new Scanner(System.in); int t = in.nextIn

斐波那契序列中的每个新项都是通过将前两项相加生成的。从和开始,前10个术语将是:

1,1,2,3,5,8,13,21,34,... 
这里我们应该找到斐波那契数列中的偶数,然后把它们加到和中

以及守则:

import java.util.*;

public class Abhi {


public static void main(String[] args) {

    Scanner in = new Scanner(System.in);
    int t = in.nextInt();
    int[] n = new int[t];

    int i,j;
    long sum;
    for(int a0 = 0; a0 < t; a0++){
        n[a0] = in.nextInt();            
    }
    //int an = n.length;
    int[] nn = new int[1000];
    nn[0]=1;
    nn[1]=2;
    for(i = 0 ; i<t;i++){
        sum = 2;
        for(j= 2;j<n[i];j++){                
            nn[j] = nn[j-2] + nn[j-1];
                if(nn[j]%2==0 && nn[j]<n[i])
                    {
                    sum += nn[j];
                 //System.out.println(sum);   
                 //the above line shows correct output 
                }
            }            
        System.out.println(sum);//this is printing different output
    }}}
样本输出:

44

您也可以通过使用以下代码来实现这一点:

import java.util.Scanner;

public class Fibo 
{
    public static void main(String[] args) 
    {
        Scanner sc = new Scanner(System.in);

        int No = sc.nextInt();

        int fNo1 = 1;
        int fNo2 = 1;
        int fNo3 = fNo1 + fNo2;
        int sumofEvenNo = 0;
        int i;
        while( fNo3 < No)
        {
            if(fNo3 % 2 == 0)
                sumofEvenNo += fNo3;
            fNo1 = fNo2;
            fNo2 = fNo3;
            fNo3 = fNo1 + fNo2;
        }

        System.out.println("Sum of all even nos = "+sumofEvenNo);
    }
}

这里的问题与外部系统无关。out.printlnsum;正如你提到的。这是因为int范围

int的最大值为2 147 483 647,在斐波那契数列中,2 971 215 073位于第47位,当它超过int范围时,会以意外的方式给出结果

在您的代码数组中,nn保持-1323752223,而不是导致问题的2971215073

要解决此问题,请使用BigInteger,如下所示

    BigInteger sum;
    BigInteger[] nn = new BigInteger[1000];
    nn[0] = new BigInteger("1");
    nn[1] = new BigInteger("2");
    for (i = 0; i < t; i++) {
        sum = new BigInteger("2");
        for (j = 2; j < n[i]; j++) {
            nn[j] = nn[j - 2].add(nn[j - 1]);
            if (nn[j].mod(new BigInteger("2")).equals(new BigInteger("0")) && 
                nn[j].compareTo(new BigInteger(String.valueOf(n[i])))<0) {
                sum = sum.add(nn[j]);
                System.out.println(sum);
            }
        }
        System.out.println(sum);
    }
我建议使用,您的代码将更加清晰,易于调试。我也用a来求和

询问将计算多少次总数。 获取索引并计算总和。 打印 类别:
注意:Fibbonaci:1,1,2,3…

您的代码和问题不清楚。你想做什么?谢谢你解释什么是斐波那契序列,但我看不出有任何问题。。。或者任何问题。PS:顺序从1,1,2开始,或者你应该设置sum=3而不是2。正确的输出是什么?为什么?我同意你的观点,这更简单,但是你没有回答问题,因为目前没有问题@Thrasher他使用的是long for sum而不是数组nn,并且nn数组包含斐波那契数。您应该突出显示您的更改。因为我仍然看到int-sum,我怀疑这会在很长一段时间内起作用
    BigInteger sum;
    BigInteger[] nn = new BigInteger[1000];
    nn[0] = new BigInteger("1");
    nn[1] = new BigInteger("2");
    for (i = 0; i < t; i++) {
        sum = new BigInteger("2");
        for (j = 2; j < n[i]; j++) {
            nn[j] = nn[j - 2].add(nn[j - 1]);
            if (nn[j].mod(new BigInteger("2")).equals(new BigInteger("0")) && 
                nn[j].compareTo(new BigInteger(String.valueOf(n[i])))<0) {
                sum = sum.add(nn[j]);
                System.out.println(sum);
            }
        }
        System.out.println(sum);
    }
import java.util.*;

public class Abhi {

    public static void main(String[] args) {

        System.out.println ( "This program will calculate the even sum of given fibonacci series index ( index starts at 1) " );
        System.out.println ( "Please enter how many times do you want to calculate the sum: " );
        Scanner scan = new Scanner(System.in);
        int iterationTimes = scan.nextInt() ;

        for (; iterationTimes > 0; iterationTimes-- )
        {
            System.out.println ( "Please, enter the index on fibonacci: " );
            System.out.println ( "Even sum: " + getEvenSum( scan.nextInt() ) );
        }

    }

    private static long getEvenSum( int index)
    {
        if ( index <= 2 )
        {
            return 0;
        }

        long n1=1, n2=1, n3, sum = 0;   

        for(int i = 2; i < index; i++) 
        {    
            n3=n1+n2;
            if ( n3 % 2 == 0)
            {
                sum += n3;
            }
            n1=n2;    
            n2=n3;    
        }  

        return sum;

    }


}
This program will calculate the even sum of given fibbonacci series index ( index starts at 1 )
Please enter how many times do you want to calculate the sum: 
3
Please, enter the index on fibbonacci: 
3
Even sum: 2
Please, enter the index on fibbonacci: 
6
Even sum: 10
Please, enter the index on fibbonacci: 
12
Even sum: 188