我需要创建一个程序,以根据用户输入的数组值输出最小,最大,平均值,总体标准偏差和样本标准偏差。
import java.util.Scanner;
public class {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner input = new Scanner(System.in);
System.out.println("How many of the type double do you want to store"
+ " in your array?: ");
int number = input.nextInt();
double[] arrayofvals = new double[number];
double total = 0;
for (int i = 0; i < number; i++){
System.out.print("Enter value " + (i+1) + " : ");
arrayofvals[i] = input.nextDouble();
}
{
}
System.out.print("----------------------------------------\nHere are "
+ "the stats for your values: \n------------------------------"
+ "----------");
double max = getMax(arrayofvals);
System.out.println("\nMaximum value is: " +max);
double min = getMin(arrayofvals);
System.out.println("\nMinimum value is: " + min);
double average = getMean (arrayofvals);
System.out.println("\nAverage of array values is: " + total);
}
public static double getMax(double[] arrayofvals){
double maxValue = arrayofvals[0];
for(int i=1;i < arrayofvals.length; i++){
if(arrayofvals[i] > maxValue){
maxValue = arrayofvals[i];
}
}
return maxValue;
}
public static double getMin(double[] arrayofvals){
double minValue = arrayofvals[0];
for(int i=1;i < arrayofvals.length; i++){
if(arrayofvals[i] < minValue){
minValue = arrayofvals[i];
}
}
return minValue;
}
public static double getMean (double[] arrayofvals){
double total = 0;
double avgVal = arrayofvals[0];
for (int i =0; i< arrayofvals.length; i++){
total = total + arrayofvals[i];
}
double average = total / arrayofvals.length;
return avgVal;
}
}
我无法超越平均计算,因为我一直得到0的回报。我不知道我在做什么错。任何帮助或建议,我们将不胜感激。
答案 0 :(得分:0)
retutn平均值,而不是avgVal;
public static double getMean (double[] arrayofvals){
double total = 0;
for (int i =0; i< arrayofvals.length; i++){
total = total + arrayofvals[i];
}
double average = total / arrayofvals.length;
return average;
}
,并以main
的平均值打印,而不是total
double average = getMean (arrayofvals);
System.out.println("\nAverage of array values is: " + average);// replaced total with average