[英]System.out.println is not printing output
Here is my code to find even Fibonacci numbers and to add them: 这是我的代码,甚至可以找到斐波那契数并添加它们:
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
}
}
}
Sample Input 样本输入
2 10 100 2 10 100
Sample Output 样本输出
10 44 10 44
The problem is that this line System.out.println(sum);
问题是此行
System.out.println(sum);
is not printing anything. 没有打印任何东西。
Any ideas? 有任何想法吗?
In your code you have 在您的代码中
for(int a0 = 0; a0 < t; a0++){
n[a0] = in.nextInt();
}
The problem is that the program is waiting for you to enter t
integers. 问题在于程序正在等待您输入
t
整数。 I don't know what values you want there, but change it to something more like this 我不知道您想要什么值,但是将其更改为类似这样的值
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.
}
I hope you find this helpful! 我希望这个对你有用!
I do not see a problem here. 我在这里看不到问题。 Just took the code, compiled and executed it.
只是拿了代码,编译并执行了它。 After specifying the value for t and also providing t input values, I saw an output on the console.
指定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)
Multiple problems exist here. 这里存在多个问题。
One error at line 25 if the t for example is 5
: 如果
t for example is 5
则在第25行出现一个错误:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at application.A.main(A.java:25)
Added some System.out.println(...)
to see how you can fix it cause i don't know the code you want to add: 添加了一些
System.out.println(...)
来查看如何解决它,因为我不知道要添加的代码:
package application; 打包申请;
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.