Java 如何将命令行参数转换为双数组以计算和?
现在我得到了一个“Sum=0.0”和一个平均值等于“NaN”,在与很多警告“可能从double到int的有损转换”的消息斗争之后。我认为代码最终采用了double,但仍然没有达到我希望的效果:从命令行获取值,将它们放入数组中,求和,然后计算平均值 你知道错误在哪里吗Java 如何将命令行参数转换为双数组以计算和?,java,Java,现在我得到了一个“Sum=0.0”和一个平均值等于“NaN”,在与很多警告“可能从double到int的有损转换”的消息斗争之后。我认为代码最终采用了double,但仍然没有达到我希望的效果:从命令行获取值,将它们放入数组中,求和,然后计算平均值 你知道错误在哪里吗 public class StudentMarks{ protected double[] marks; //create an array filled with double values public StudentMar
public class StudentMarks{
protected double[] marks;
//create an array filled with double values
public StudentMarks(double[] marks){
this.marks = new double[0]; //set the default array size
}
public void setMarks(){
this.marks = marks;
}
public void getArray(){
//one can only print arrays using loops..
//took me a little to realise that erm.
for(int i=0; i<marks.length; i++)
System.out.println(marks[i]);
}
public double calSum(){
double totals = 0.0;
for(double i: marks) {
//double mLength = Double.parseDouble(marks[i]);
totals+= i;
}
return totals;
}
//A method to calculate the mean of all elements
public double calMean(){
double means = (calSum()/marks.length);
return means;
}
//A main method to test
public static void main(String[] args) {
// Check to see if the user has actually sent a paramter to the method
if (args.length != 7){
System.out.println("Usage: java RandomArray <NUM>. Example: java RandomArray 5");
System.exit(-1);
}
double[] prompt = new double[args.length];
for (int i =0; i<args.length; i++){
prompt[i] = Double.parseDouble(args[i]);
}
StudentMarks test = new StudentMarks(prompt);
test.getArray();
// Calculate the sum of all the values in the array and print it
System.out.println("Sum: "+ test.calSum());
// Calculate the mean of all the values in the array and print it
System.out.println("Mean: "+ test.calMean());
}
}
公共班级学生分数{
受保护的双[]标记;
//创建一个填充有双值的数组
公共学生分数(双[]分){
this.marks=new double[0];//设置默认数组大小
}
公共无效设置标记(){
这个。标记=标记;
}
public void getArray(){
//只能使用循环打印阵列。。
//我花了一点时间才意识到这一点。
对于(int i=0;i而不是
this.marks = new double[0];
public StudentMarks(double[] marks){
this.marks = new double[0];
}
使用
您当前正在将marks
成员变量指定为零长度数组,而不是参数,因此元素之和为零,marks.length
为零,因此calSum()/marks.length
为0.0/0.0
,定义为NaN
类的初始值设定项中存在一个问题。该类当前初始化为0长度数组。您应该使用输入对其进行初始化
使用
而不是
this.marks = new double[0];
public StudentMarks(double[] marks){
this.marks = new double[0];
}
这是代码的一个固定版本。为了清晰起见,请查看内联注释
public class StudentMarks{
protected double[] marks;
//create an array filled with double values
//Pass in the array of marks to initialize the class
public StudentMarks(double[] marks){
this.marks = marks; //set the marks array in the class to the passed in one
}
//Set the class marks variable to the passed in one
public void setMarks(double[] marks){
this.marks = marks;
}
//Change the name to "printMarks" to better reflect the purpose of the method
public void printMarks(){
//one can only print arrays using loops..
//took me a little to realise that erm.
for(int i=0; i<marks.length; i++){
System.out.println(marks[i]);
}
}
//
public double calSum(){
double totals = 0.0;
for(double i: marks) {
//double mLength = Double.parseDouble(marks[i]);
totals+= i;
}
return totals;
}
//A method to calculate the mean of all elements
public double calMean(){
double means = (calSum()/marks.length);
return means;
}
//A main method to test
public static void main(String[] args) {
//Print out an error and exit only if we have less than 1 element passed in
if (args.length != 7){
System.out.println("Usage: java RandomArray <NUM>. Example: java RandomArray 5");
System.exit(-1);
}
double[] prompt = new double[args.length];
//Note that there is no error checking here
for (int i =0; i<args.length; i++){
prompt[i] = Double.parseDouble(args[i]);
}
//Initialize the StudentMarks class with the value of the input
StudentMarks test = new StudentMarks(prompt);
test.printMarks();
// Calculate the sum of all the values in the array and print it
System.out.println("Sum: "+ test.calSum());
// Calculate the mean of all the values in the array and print it
System.out.println("Mean: "+ test.calMean());
}
}
公共班级学生分数{
受保护的双[]标记;
//创建一个填充有双值的数组
//传入标记数组以初始化类
公共学生分数(双[]分){
this.marks=marks;//将类中的marks数组设置为传入的数组
}
//将class marks变量设置为传入变量
公共无效设置标记(双[]标记){
这个。标记=标记;
}
//将名称更改为“printMarks”,以更好地反映方法的用途
公共作废印刷品(){
//只能使用循环打印阵列。。
//我花了一点时间才意识到这一点。
for(int i=0;idouble[]prompt=new double[args.length];
将始终具有长度1,因为您选中了if(args.length!=1){…}
就在前面。请定义您的输入格式。您说输入是一个双精度数组,但您只允许有一个命令行参数。请澄清。我做了一个小的编辑,允许从命令行输入更多的数字,因此不需要分隔或删除参数double[]标记(<代码)>从构造函数(如果您希望保持标准初始化),并将<代码> SETMARKSH()/代码>改为<代码> SETMARK(double []标记)< /> >(这是无论如何SET方法应该使用的),并在您的主()/<代码>方法中使用它。@ MEL不担心-如果有用的话,请考虑接受答案。“只要传入多个参数,应用程序就会中断。"在您发布此消息时,OP已将条件更新为!=7
;OP对此似乎有一个非常具体的要求。感谢您的提示@Andy Turner。在我编写代码时,OP仍然有!=1
,因此我做了一个假设,并对其进行了更改。不过,您是正确的,看起来他们有一个特定的要求。我很抱歉我编辑了我的代码来匹配它。“上面的代码实际上有几个问题。”现在你删除了length<1
,这是真的吗?我倾向于避免发布大量的代码,因为不太清楚除了构造函数之外,你在语义上改变了什么(以及为什么)(您必须上下滚动页面才能看到更改,这不是很容易)。不要忘记,对你来说更清晰的东西不一定对其他阅读它的人来说更清晰。再次编辑以反映OP中的更改。我选择发布完整的代码是由两个因素驱动的。首先,OP看起来是由一位新工程师编写的,我不确定指向一行是否会对他们有所帮助。这样,他们就有了一个完整的代码要比较的代码。我的第二个原因是,代码中有不止一个问题,现在是沉默的。不过,我非常欣赏批评,并将在将来记住这一点。