如何将命令行参数转换为双数组以计算总和?

时间:2015-12-11 15:45:32

标签: java

所以目前我得到一个" Sum = 0.0"并且平均值等于" NaN",在打了很多消息之后再次发出警告"可能有损转换从double到int"。我认为代码最终会占用双倍,但仍然没有按照我的意愿执行:从命令行获取值,将它们放入数组中,对它们求和,然后计算均值。

错误所在的任何想法?

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());
    }

}

2 个答案:

答案 0 :(得分:3)

而不是

this.marks = new double[0];

使用

this.marks = marks;

您当前正在将marks成员变量指定为零长度数组而不是参数,因此元素之和为零,marks.length为零,因此{{1} }是calSum()/marks.length,定义为0.0 / 0.0

答案 1 :(得分:0)

一个问题出在班级的初始化者身上。该类当前初始化为0长度数组。相反,你应该用你的输入初始化它。

使用

public StudentMarks(double[] marks){
    this.marks = marks;   
}

而不是

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());
}

}