Java 将void方法调用到构造函数中

Java 将void方法调用到构造函数中,java,Java,我有一个这样的问题。用户在数组中输入5个等级。 然后必须将数组从最高到最低排序,然后求平均值 selectionSort和calculateMean的方法必须为空 如何将它们调用到构造函数中?然后我需要将数据打印到toString 这就是我目前所拥有的 import java.util.Scanner; public class Average { //the array which will contain the scores private int data[]; //the aver

我有一个这样的问题。用户在数组中输入5个等级。 然后必须将数组从最高到最低排序,然后求平均值

selectionSort和calculateMean的方法必须为空

如何将它们调用到构造函数中?然后我需要将数据打印到toString

这就是我目前所拥有的

import java.util.Scanner;

public class Average {

//the array which will contain the scores
private int data[];
//the average of the scores
private double mean;



public Average(){
    Scanner sc = new Scanner(System.in);

    double data[] = new double[6];

    for(int i = 1; i < data.length; i++){
        System.out.println("Enter score number " + i);
        data[i] = sc.nextDouble();

    }

    /*for(int p = 1;  p < data.length; p ++){
        System.out.println(data[p]);
    }*/
    //selectionSort();




}
public void calculateMean(){
    double total = 0;
    double sum = 0;
    double average = 0;

            for(int counter = 0; counter < data.length; counter++){
                sum = sum + data[counter];
                average = sum / data.length; 

            }

}


public void selectionSort(){
    int temp = 0;
    for(int joey = 0; joey<data.length; joey++){    
        for(int  i = 1; i < data.length; i++) {
            if(data[i - 1] > data[i]) {
            temp = data[i-1];
            data[i-1] = data[i];
            data[i] = temp;
            }
        }
    }
         for(int p = 0; p < data.length; p++){
            System.out.println(data[p]);

    }


    }   
public String toString(){



    return null;
}
}
import java.util.Scanner;
公共课平均分{
//将包含分数的数组
私有整数数据[];
//平均分数
私人双平均;
公共海损(){
扫描仪sc=新的扫描仪(System.in);
双精度数据[]=新双精度数据[6];
对于(int i=1;i
您可以像这样调用构造函数中的两个方法

// Inside constructor
selectionSort(); // call the method
calculateMean(); // call the method
System.out.println(toString()); // print the data using toString
public String toString() {
    return Arrays.toString(data);
}
data[] = new int[5];
您的
toString()
可以如下所示

// Inside constructor
selectionSort(); // call the method
calculateMean(); // call the method
System.out.println(toString()); // print the data using toString
public String toString() {
    return Arrays.toString(data);
}
data[] = new int[5];
另外,
calculateMean()
中的逻辑有点错误

for (int counter = 0; counter < data.length; counter++) {
    sum = sum + data[counter];
}
average = sum / data.length; // Calculate the average after finding the sum and not at every iteration.
下面的
for
循环获取用户的输入,从索引
0
开始,而不是
1

for(int i = 0; i < data.length; i++){
for(int i=0;i
Why the downvote???。这家伙尝试了一些东西……不太清楚你所说的“调用我的构造函数”是什么意思。请注意,通常,构造函数只是初始化类的变量,而用于获取用户输入等操作的代码会进入其他方法(或者对于像这样的简单程序,
main
)。或
System.out.println(this);
如果他适当地重写
toString()
。@chrylis-当然可以。但是我想既然OP想要调用这些方法,最好通过调用
toString()来打印数据
显式调用,而不是仅通过使用
this
@R.J隐式调用。好的,我已经调用了方法并完成了toString。但是我如何确切地获得calculateMean()的结果呢存储到私有的double mean;?@user3056312-在计算平均值之后,在方法本身中,您可以做,
mean=average;
@RJ-wow,这让我觉得很笨。好的,所以每当我这样做时,我都会得到这个。线程“main”java.lang.NullPointerException中的异常平均值。calculateMean(平均值。java:44)(平均值。java:31)位于AverageDriver.main(AverageDriver.java:7)