所以我有一个作业,我应该创建一个程序,该程序生成100个随机整数并将它们存储在一个一维数组中。然后,我应该计算平均值,标准偏差和方差。通过大量的试验和错误,我能够制作出一个执行此操作的程序,但是我错过的是它说为每个属性(平均值,标准偏差和方差)编写一个单独的方法。我该怎么做?
package homeassignment5;
public class HomeAssignment5 {
public static void main(String[] args) {
int n;
n = 100;
int[] random = new int [n];
for (int i = 0; i<random.length; i++)
random[i] = (int) (Math.random()* n);
double total = 0;
double average = 0;
double variance = 0;
double var = 0;
double sd = 0;
for (int element : random){
total += element;
average = total/n;
}
for (int i = 0; i<random.length; i++){
variance += (random[i] - average) * (random[i] - average);
}
var = variance / random.length;
sd = Math.sqrt(var);
//System.out.println("Total is: " + total);
System.out.println("Average is: " + average);
System.out.println("Variance is " + var);
System.out.println("Standard deviation is " + sd);
/*for (int i = 0; i<random.length; i++)
System.out.println(random[i]);
*/
}
}
可能有点混乱,但是我对Java真的很陌生,这是大量试验和错误的产物。
答案 0 :(得分:0)
我相信这会对您有所帮助。 所有功能都以方法形式分开, 初始化是分开的。
**欢迎使用**
public class Refactor {
private int[] random;
private int n;
private double total = 0;
private double average = 0;
private double variance = 0;
private double sd = 0;
public Refactor(int n) {
this.n = n;
this.populateArray();
}
private void populateArray() {
random = new int[n];
for (int i = 0; i < random.length; i++)
random[i] = (int) (Math.random() * n);
}
public double getAverage() {
average = 0;
total = 0;
for (int element : random) {
total += element;
}
average = total / n;
return average;
}
public double getVariance() {
variance = 0;
for (int i = 0; i < random.length; i++) {
variance += (random[i] - average) * (random[i] - average);
}
variance = variance / random.length;
return variance;
}
public double getSD() {
this.getVariance();
sd = 0;
sd = Math.sqrt(variance);
return sd;
}
public static void main(String[] args) {
Refactor ref = new Refactor(100);
System.out.println("Average is: " + ref.getAverage());
System.out.println("Variance is " + ref.getVariance());
System.out.println("Standard deviation is " + ref.getSD());
}
}
答案 1 :(得分:0)
因此,有几种解决方法,但是最简单的方法可能是:
public class HomeAssignment5 {
private static int[] random;
public static void main(String[] args){
random = = new int[100];
...
}
public static void getAverage(){
// void if you want to print it here, double if you want to print in main
double average = 0;
int total = 0;
for(int i = 0; i < 100; i += 1)
total += random[i];
average = total / n; // you'll want to do this outside of the loop to
// to get the correct result!
System.out.println("Average: " + average);
}
...
}
您也可以将数组作为参数传递给每个方法。
顺便说一句,在这里发布的内容中,您实际上尚未向该数组中写入任何随机数,因此请记住要这样做,它目前仍为零。
答案 2 :(得分:0)
如何将数组传递给每个方法?
这三种方法中第一个也是最简单的方法是average
:
static double average(int [] r)
{ double total = 0;
for (int e: r) total += e;
return total/r.length;
}
对于第二种方法,标准偏差,我建议不要传递数组,而应传递方差,因为如果程序无论如何都计算了方差,则无需重新计算,如果没有,则可以使用对方差方法的调用作为参数来调用SD方法。
static double SD(double variance) { return Math.sqrt(variance); }
对于第三个方法,即方差,我建议不仅传递数组,而且还要传递其平均值,其推理与上述类似。
static double variance(int [] r, double average)
{ double var = 0;
for (int e: r) var += (e - average) * (e - average);
return var/r.length;
}
定义了这三种方法后,您现在可以替换
double total = 0;
double average = 0;
double variance = 0;
double var = 0;
double sd = 0;
for (int element : random){
total += element;
average = total/n;
}
for (int i = 0; i<random.length; i++){
variance += (random[i] - average) * (random[i] - average);
}
var = variance / random.length;
sd = Math.sqrt(var);
使用
double average = average(random);
double var = variance(random, average);
double sd = SD(var);