java 帮助中的命令行参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5098318/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CommandLine Arguments in java Help
提问by Mugetsu
I'm suppose to use Command-Line Arguments to take user input and than use an enhanced for loop to sum.
我假设使用命令行参数来获取用户输入,而不是使用增强的 for 循环来求和。
This is the error:
这是错误:
Exception in thread "main" java.lang.Error: Unresolved compilation problem: Type mismatch: cannot convert from double to int
线程“main”中的异常java.lang.Error:未解决的编译问题:类型不匹配:无法从double转换为int
public class EnhanceForLoop {
public static void main(String[] args) {
// TODO Auto-generated method stub
if(args.length !=5)
System.out.println(" please enter no more than 4 numbers");
else
{
double sum;
double arrayLength = Double.parseDouble(args[0]);
double [] myArray = new double [ arrayLength ];
double value = Double.parseDouble((args[1]));
double counter = Double.parseDouble((args[2]));
for(double num: myArray)
sum += num;
System.out.printf("The sum is %f ", sum);
}
}
}
here is how it is so far:
这是到目前为止的情况:
public class EnhanceForLoop {
公共类 EnhanceForLoop {
public static void main(String[] args) {
// TODO Auto-generated method stub
if(args.length !=5)
System.out.println(" please enter no more than 4 numbers");
else
{
double sum = 0.0;
int arrayLength = Integer.parseInt(args[0]);
double [] myArray = new double [ arrayLength ];
double num1 = Double.parseDouble((args[1]));
double num2 = Double.parseDouble((args[2]));
double num3 = Double.parseDouble((args[3]));
double num4 = Double.parseDouble((args[4]));
double num5 = Double.parseDouble((args[5]));
for(double num: myArray)
sum += num;
System.out.printf("The sum is %f ", sum);
}
}
}
}
Here is the answer:
这是答案:
public class EnhanceForLoop {
公共类 EnhanceForLoop {
public static void main(String[] args) {
// TODO Auto-generated method stub
if(args.length !=5)
System.out.println(" please enter no more than 4 numbers");
else
{
double sum = 0.0;
int arrayLength = Integer.parseInt(args[0]);
double [] myArray = new double [ arrayLength ];
double num1 = Double.parseDouble((args[1]));
double num2 = Double.parseDouble((args[2]));
double num3 = Double.parseDouble((args[3]));
double num4 = Double.parseDouble((args[4]));
for(String s: args){
sum += Double.parseDouble(s);
}
System.out.println("Sum: "+sum);
}
}
}
}
采纳答案by Pa?lo Ebermann
You are summing up an array filled with 0.0
(as this is the default value), not the command line arguments.
您正在总结一个填充的数组0.0
(因为这是默认值),而不是命令行参数。
If you want to sum the arguments, you have to iterate over them (=the args
array), convert each argument to a double, and add these up. You don't need the double[]
at all.
如果你想对参数求和,你必须迭代它们(=args
数组),将每个参数转换为双精度值,然后将它们相加。你根本不需要double[]
。
Edit: (after some comments)
编辑:(一些评论后)
In your example code of your question you are using an enhanced for-loop of an array, so it seems you know what such a loop is. So, now use the args
array instead of your myArray
there. Inside the loop, you do the Integer.parseInt(...)
or Double.parseDouble
or similar, and add the result to your sum
variable.
在您问题的示例代码中,您使用的是数组的增强 for 循环,因此您似乎知道这样的循环是什么。所以,现在使用args
数组而不是你的myArray
那里。在循环中,你做Integer.parseInt(...)
或Double.parseDouble
或类似的,结果添加到您的sum
变量。
If you need more than one statement in the loop, use { ... }
to group them.
如果循环中需要多个语句,请使用{ ... }
将它们分组。
回答by Jim Garrison
arrayLength
must be an integer. Thus
arrayLength
必须是整数。因此
int arrayLength = Integer.parseInt(args[0]);
double [] myArray = new double [ arrayLength ];
回答by Jason Sendros
It's also important to remember to think about the flow of your code:
记住考虑代码的流程也很重要:
You initialize an array. You sum the values in the array.
你初始化一个数组。您对数组中的值求和。
You never added values to the array, so you will always be summing a bunch of zeros.
您从未向数组添加值,因此您将始终对一堆零进行求和。