What is the error in following code while trying command line arguments ? I am getting an error at line System.out.println(args[i]);
在尝试命令行参数时,以下代码中的错误是什么?我在System.out.println(args [i])行收到错误;
public class CommandLA{
public static void main(String []args)
{
int s = 0;
for(int i=0;i<args.length;i++)
System.out.println(args[i]);
s = s + Integer.parseInt(args[i]);
System.out.println("Sum is : "+s);
}
}
3 个解决方案
#1
2
maybe
public static void main(String []args)
{
int s = 0;
for (String str : args) {
s = s + Integer.parseInt(str);
}
System.out.println("Sum is : "+s);
}
or using an indexed for
或使用索引
public static void main(String []args)
{
int s = 0;
for (int i = 0; i < args.length; i++) {
s = s + Integer.parseInt(args[i]);
}
System.out.println("Sum is : "+s);
}
#2
1
Simple:
for(int i=0;i<args.length;i++)
System.out.println(args[i]);
followed by
s = s + Integer.parseInt(args[i]);
But - you are missing the { after the loop! Therefore the scope in which i exists (is visible) is only the line directly after the "for-loop" line!
但是 - 你错过了{循环之后!因此,i存在的范围(可见)只是“for-loop”行之后的行!
In other words you need for (..) { all stuff that uses i }!
换句话说,你需要(..){所有使用我的东西}!
#3
0
To elaborate on the answer of GhostCat:
详细说明GhostCat的答案:
for(int i=0;i<args.length;i++)
System.out.println(args[i]);
s = s + Integer.parseInt(args[i]);
is the same as
是相同的
for(int i=0;i<args.length;i++){
System.out.println(args[i]);
}
s = s + Integer.parseInt(args[i]);
Which means that in the last line, i is not known, resulting in an error.
这意味着在最后一行,我不知道,导致错误。
I wonder why the error was detected at the line before, because until that, the code is technically correct.
我想知道为什么之前在该行检测到错误,因为在此之前,代码在技术上是正确的。
That said, I recommend to use brackets in any case. Some people omit them to get shorter code, but that means that if one adds a line later on, he could easily make a mistake. This is personal preference, of course.
也就是说,我建议在任何情况下都使用括号。有些人省略了它们以获得更短的代码,但这意味着如果稍后添加一行,他很容易犯错误。当然,这是个人偏好。
#1
2
maybe
public static void main(String []args)
{
int s = 0;
for (String str : args) {
s = s + Integer.parseInt(str);
}
System.out.println("Sum is : "+s);
}
or using an indexed for
或使用索引
public static void main(String []args)
{
int s = 0;
for (int i = 0; i < args.length; i++) {
s = s + Integer.parseInt(args[i]);
}
System.out.println("Sum is : "+s);
}
#2
1
Simple:
for(int i=0;i<args.length;i++)
System.out.println(args[i]);
followed by
s = s + Integer.parseInt(args[i]);
But - you are missing the { after the loop! Therefore the scope in which i exists (is visible) is only the line directly after the "for-loop" line!
但是 - 你错过了{循环之后!因此,i存在的范围(可见)只是“for-loop”行之后的行!
In other words you need for (..) { all stuff that uses i }!
换句话说,你需要(..){所有使用我的东西}!
#3
0
To elaborate on the answer of GhostCat:
详细说明GhostCat的答案:
for(int i=0;i<args.length;i++)
System.out.println(args[i]);
s = s + Integer.parseInt(args[i]);
is the same as
是相同的
for(int i=0;i<args.length;i++){
System.out.println(args[i]);
}
s = s + Integer.parseInt(args[i]);
Which means that in the last line, i is not known, resulting in an error.
这意味着在最后一行,我不知道,导致错误。
I wonder why the error was detected at the line before, because until that, the code is technically correct.
我想知道为什么之前在该行检测到错误,因为在此之前,代码在技术上是正确的。
That said, I recommend to use brackets in any case. Some people omit them to get shorter code, but that means that if one adds a line later on, he could easily make a mistake. This is personal preference, of course.
也就是说,我建议在任何情况下都使用括号。有些人省略了它们以获得更短的代码,但这意味着如果稍后添加一行,他很容易犯错误。当然,这是个人偏好。