How do i print out a set of integers in an args array. I intend to have an output of: The numbers are 1, 2, 3, 4, 5
如何在args数组中打印出一组整数。我打算输出:数字是1,2,3,4,5
System.out.println("The numbers are ");
for(int i=0; i< args.length;i++)
System.out.print(args[i] + " ";
The compiler gives me an error of "i cannot be resolved to a variable" When i resolve it with "int b = 0;" it gives me a single integer, rather than the integers in args
编译器给我一个错误“我无法解析为变量”当我用“int b = 0;”解决它时它给了我一个整数,而不是args中的整数
4 个解决方案
#1
3
As of Java 5 you can also skip i
entirely:
从Java 5开始,你也可以完全跳过我:
for( String arg : args )
System.out.print(arg + " ");
Additionally, I'd like to advocate using curly braces even for one-statement blocks. It's easier to read and less likely to change the behavior by adding a statement (like some log statement).
另外,我想提倡使用花括号,即使是单语句块。通过添加语句(如某些日志语句)更容易阅读并且更不可能更改行为。
Example, a naive pow()
method (checks skipped for simplicity):
例如,一个天真的pow()方法(为简单而跳过检查):
Orignal code:
原始代码:
int pow(int base, int power) { //assume power to be > 1
int result = base;
for( int i = 1; i <= power; i++ )
result *= base;
return result;
}
Now I'd like to log the result in each step:
现在我想在每一步中记录结果:
int pow(int base, int power) { //assume power to be > 1
int result = base;
for( int i = 1; i <= power; i++ )
System.out.println( base + " pow( " + i + ") = " + result );
result *= base;
return result;
}
Suddenly, I get a wrong result for power > 2
, since now result *= base;
is only executed once (it is now outside the loop).
突然间,我得到一个错误的结果,因为现在结果* = base;只执行一次(它现在在循环之外)。
#2
3
If that is your actual code, you are missing an ending parenthesis in the last line before the semi-colon.
如果这是您的实际代码,则在分号前的最后一行中缺少结束括号。
This works for me:
这对我有用:
public class tester {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("The numbers are ");
for (int i = 0; i < args.length; i++)
System.out.print(args[i] + " ");
}
}
Output (when 1 2 3 4 5 are specified on the cmd line):
输出(在cmd行上指定1 2 3 4 5时):
The numbers are
1 2 3 4 5
#3
2
Forgot parenthesis?
忘了括号?
System.out.print(args[i] + " ");
#4
1
You forgot to close your println
method with a );
.
你忘了用a)关闭你的println方法;
Your problem:
你的问题:
System.out.print(args[i] + " ";
Your solution:
你的解决方案
System.out.print(args[i] + " ");
#1
3
As of Java 5 you can also skip i
entirely:
从Java 5开始,你也可以完全跳过我:
for( String arg : args )
System.out.print(arg + " ");
Additionally, I'd like to advocate using curly braces even for one-statement blocks. It's easier to read and less likely to change the behavior by adding a statement (like some log statement).
另外,我想提倡使用花括号,即使是单语句块。通过添加语句(如某些日志语句)更容易阅读并且更不可能更改行为。
Example, a naive pow()
method (checks skipped for simplicity):
例如,一个天真的pow()方法(为简单而跳过检查):
Orignal code:
原始代码:
int pow(int base, int power) { //assume power to be > 1
int result = base;
for( int i = 1; i <= power; i++ )
result *= base;
return result;
}
Now I'd like to log the result in each step:
现在我想在每一步中记录结果:
int pow(int base, int power) { //assume power to be > 1
int result = base;
for( int i = 1; i <= power; i++ )
System.out.println( base + " pow( " + i + ") = " + result );
result *= base;
return result;
}
Suddenly, I get a wrong result for power > 2
, since now result *= base;
is only executed once (it is now outside the loop).
突然间,我得到一个错误的结果,因为现在结果* = base;只执行一次(它现在在循环之外)。
#2
3
If that is your actual code, you are missing an ending parenthesis in the last line before the semi-colon.
如果这是您的实际代码,则在分号前的最后一行中缺少结束括号。
This works for me:
这对我有用:
public class tester {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("The numbers are ");
for (int i = 0; i < args.length; i++)
System.out.print(args[i] + " ");
}
}
Output (when 1 2 3 4 5 are specified on the cmd line):
输出(在cmd行上指定1 2 3 4 5时):
The numbers are
1 2 3 4 5
#3
2
Forgot parenthesis?
忘了括号?
System.out.print(args[i] + " ");
#4
1
You forgot to close your println
method with a );
.
你忘了用a)关闭你的println方法;
Your problem:
你的问题:
System.out.print(args[i] + " ";
Your solution:
你的解决方案
System.out.print(args[i] + " ");