我不知道如何在没有这种方法的情况下获得中位数,但它一直在说转换错误。我知道这是因为我将numbers.length除以2,但我无法弄清楚如何让程序理解它是双倍的。它位于我不断收到错误的底部。这是我最后的if else声明。我到处搜索所以如果这是重复我很抱歉,但我找不到一个。我无法转换为整数,因为我的数组包含双精度数。提前感谢帮助我的人。
import static java.lang.Math.*;
import java.util.Scanner;
import java.util.Arrays;
public class ComputingArrays
{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
double [] numbers = {33.5, 67.5, 67.5, 88.0, 46.0, 94.5, 77.5, 83.0, 95.0, 80.5}; // declare, allocate memory, and store values
double n = numbers.length; // get length of array
Arrays.sort(numbers);
System.out.println("The Array of Numbers are :");
double sum = 0; //initiate and declare sum
for (int i = 0; i < n; i++) { //loop through n numbers
System.out.println(numbers[i]);
sum = sum + numbers[i];
}
final double average = sum / n;
System.out.println("The Sum of the Array is " + sum);
System.out.println("The Average of the Array is " + average);
double var1 = 0;
double var2 = 0;
double varSum = 0;
for (int i = 0; i < n; i++) {
var1 = numbers[i] - average;
var2 = var1 * var1; // to square
varSum = varSum + var2; // to get sum of all squared differences
}
double variance = varSum / n;
System.out.printf("The Variance of the Array is %.2f %n", variance); //%.2f rounds two decimal places
System.out.printf("The Standard Deviation is %.3f %n", Math.sqrt(variance));
double middle = numbers.length / 2.0;
double median = 0.0;
if (numbers.length % 2 == 1){
median = numbers[middle];
System.out.println("The median is " + median);
}
else {
median = (numbers[middle-1] + numbers[middle]) / 2;
System.out.println("The median is " + median);
}
}
}
答案 0 :(得分:1)
你应该将变量n设为int .Array length总是为int 你的问题是你正在使变量middle double.later将该变量称为索引号。但索引号只能是int。
答案 1 :(得分:1)
索引数组时,它需要int
而不是double
,因为5.5
之类的值不是有效索引。
<强>有效强>
int middle = numbers.length / 2;
System.out.println(numbers[middle]);
<强>无效强>
double middle = numbers.length / 2.0;
System.out.println(numbers[middle]);
答案 2 :(得分:0)
import static java.lang.Math.*;
import java.util.Scanner;
import java.util.Arrays;
public class HelloWorld
{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
double [] numbers = {33.5, 67.5, 67.5, 88.0, 46.0, 94.5, 77.5, 83.0, 95.0, 80.5}; // declare, allocate memory, and store values
double n = numbers.length; // get length of array
Arrays.sort(numbers);
System.out.println("The Array of Numbers are :");
double sum = 0; //initiate and declare sum
for (int i = 0; i < n; i++) { //loop through n numbers
System.out.println(numbers[i]);
sum = sum + numbers[i];
}
final double average = sum / n;
System.out.println("The Sum of the Array is " + sum);
System.out.println("The Average of the Array is " + average);
double var1 = 0;
double var2 = 0;
double varSum = 0;
for (int i = 0; i < n; i++) {
var1 = numbers[i] - average;
var2 = var1 * var1; // to square
varSum = varSum + var2; // to get sum of all squared differences
}
double variance = varSum / n;
System.out.printf("The Variance of the Array is %.2f %n", variance); //%.2f rounds two decimal places
System.out.printf("The Standard Deviation is %.3f %n", Math.sqrt(variance));
double middle = numbers.length / 2.0;
double median = 0.0;
int middleIndex = (int)middle;
if (numbers.length % 2 == 1){
median = numbers[middleIndex];
System.out.println("The median is " + median);
}
else {
median = (numbers[middleIndex-1] + numbers[middleIndex]) / 2;
System.out.println("The median is " + median);
}
}
}
打印出来:
阵列的总和是733.0
阵列的平均值为73.3
数组的方差为369.66
标准差为19.227
中位数为79.0