有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java系统。出来println不是打印输出

下面是我查找偶数斐波那契数并添加它们的代码:

 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
    }
}
}

样本输入

二, 10 一百

样本输出

十, 44

问题是这行System.out.println(sum);没有打印任何内容

有什么想法吗


共 (3) 个答案

  1. # 1 楼答案

    在您的代码中

    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.            
    }
    

    我希望这对你有帮助

  2. # 2 楼答案

    我看这里没有问题。只需获取代码,编译并执行它。在为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)
    
  3. # 3 楼答案

    这里存在多个问题

    如果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); 
            }
        }
    }