繁体   English   中英

System.out.println不打印输出

[英]System.out.println is not printing output

这是我的代码,甚至可以找到斐波那契数并添加它们:

 package a;
 import java.util.*;

public class A {

 //this about finding Even Fibonacci numbers and adding them to sum.

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int t = in.nextInt();
    int[] n = new int[t];
    int[] nn = new int[t];
    int i,j,sum;
    for(int a0 = 0; a0 < t; a0++){
        n[a0] = in.nextInt();            
    }
    nn[0]=1;
    for(i = 0 ; i<t;i++){
        sum = 0;
        for(j= 1;j<n[i];j++){                
            nn[j] = j+nn[j-1];
            if(nn[j]%2==0)
                {
                sum += nn[j];
                }
            }            
        System.out.println(sum); //this is not printing the output
    }
}
}

样本输入

2 10 100

样本输出

10 44

问题是此行System.out.println(sum); 没有打印任何东西。

有任何想法吗?

在您的代码中

for(int a0 = 0; a0 < t; a0++){
    n[a0] = in.nextInt();            
}

问题在于程序正在等待您输入t整数。 我不知道您想要什么值,但是将其更改为类似这样的值

for(int a0 = 0; a0 < t; a0++){
    n[a0] = 0;//But instead of 0 the actual number that you want to set for the value.            
}

我希望这个对你有用!

我在这里看不到问题。 只是拿了代码,编译并执行了它。 指定t的值并提供t输入值后,我在控制台上看到了输出。

stefan@linux-3047:~$ java A
5 (t)
1 (1st of 5 values)
2 (2nd of 5 values)
3 (3rd of 5 values) 
4 (4th of 5 values) 
5 (5th of 5 values) 
0 (System.out.println)
2 (System.out.println)
6 (System.out.println)
6 (System.out.println)
6 (System.out.println)

这里存在多个问题。

如果t for example is 5则在第25行出现一个错误:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
    at application.A.main(A.java:25)

添加了一些System.out.println(...)来查看如何解决它,因为我不知道要添加的代码:

打包申请;

import java.util.*;

public class A {

    // This about finding Even Fibonacci numbers and adding them to sum.

    public static void main(String[] args) {

        System.out.println("Enter a number below:\n");
        Scanner in = new Scanner(System.in);
        int t = in.nextInt();
        System.out.println("You entered..." + t+"\n");

        // Arrays
        int[] n = new int[t];
        int[] nn = new int[t];
        int i, j, sum;

        // First For Loop
        for (int a0 = 0; a0 < t; a0++) {
            System.out.println("Enter a number a0..");
            n[a0] = in.nextInt();
            System.out.println("You entered ao=:" + a0+"\n");
        }
        nn[0] = 1;

        // Second For Loop
        for (i = 0; i < t; i++) {
            sum = 0;
            for (j = 1; j < n[i]; j++) {
                nn[j] = j + nn[j - 1];
                if (nn[j] % 2 == 0) {
                    sum += nn[j];
                }
            }

            // this is not printing the output
            System.out.println("Sum is:="+sum); 
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM