부동 소수점 값 집합 (총 10 개)을 읽고 그 값의 평균, 표준 편차, 가장 작은 값, 가장 큰 값을 계산하고 표시하는 프로그램을 작성해야합니다 값 중 두 번째로 큰 값 루프가 있어야합니다. 루프에서 숫자 (소수 부분을 가질 수있는 부동 숫자)를 입력하고 변수에 숫자를 저장하라는 프롬프트를 표시합니다 (이중 유형 사용). 나는 그것을했지만 루프를 통해 계산되는 동안 변수를 저장하는 방법을 모르기 때문에 값을 쉽게 받아 들일 수있는 루프를 구현하는 데 문제가 있습니다. 이제는 코드가 매우 추해 보입니다.표준 편차 Java
import java.util.Scanner;
public class Statistics {
public static void main(String[] args){
Scanner s = new Scanner(System.in);
float average = 0;
float smallest = 0;
float largest = 0;
float scndLargest = 0;
float a, b, c, d, e, f, g, h, i, j = 0;
System.out.println("Enter values.");
a = s.nextFloat();
b = s.nextFloat();
c = s.nextFloat();
d = s.nextFloat();
e = s.nextFloat();
f = s.nextFloat();
g = s.nextFloat();
h = s.nextFloat();
i = s.nextFloat();
j = s.nextFloat();
average = (a + b + c + d + e + f + g + h + i + j)/10;
float min1 = Math.min(a, b);
float min2 = Math.min(c, d);
float min3 = Math.min(e, f);
float min4 = Math.min(g, h);
float min5 = Math.min(i, j);
float min6 = Math.min(min1, min2);
float min7 = Math.min(min3, min4);
float min8 = Math.min(min7, min5);
smallest = Math.min(min6, min8);
System.out.println("The smallest value is: " + smallest);
float max1 = Math.max(a, b); //2
float max2 = Math.max(max1, c);
float max3 = Math.max(max2, d); //4
float max4 = Math.max(max3, e);
float max5 = Math.max(max4, f); //6
float max6 = Math.max(max5, g); //6
float max7 = Math.max(max6, h); //8
float max8 = Math.max(max7, i);
largest = Math.max(max8, j); //10
System.out.println("The largest value is: " + largest);
scndLargest = Math.min(largest, max8);
System.out.println("The second largest value is: " + scndLargest);
System.out.println("The average of all the values is: " + average);
double a1 = Math.pow(a - average, 2);
double b1 = Math.pow(a - average, 2);
double c1 = Math.pow(a - average, 2);
double d1 = Math.pow(a - average, 2);
double e1 = Math.pow(a - average, 2);
double f1 = Math.pow(a - average, 2);
double g1 = Math.pow(a - average, 2);
double h1 = Math.pow(a - average, 2);
double i1 = Math.pow(a - average, 2);
double j1 = Math.pow(a - average, 2);
double sum1 = Math.pow(a1 /10, 2);
double sum2 = Math.pow(b1 /10, 2);
double sum3 = Math.pow(c1 /10, 2);
double sum4 = Math.pow(d1 /10, 2);
double sum5 = Math.pow(e1 /10, 2);
double sum6 = Math.pow(f1 /10, 2);
double sum7 = Math.pow(g1 /10, 2);
double sum8 = Math.pow(h1 /10, 2);
double sum9 = Math.pow(i1 /10, 2);
double sum10 = Math.pow(j1 /10, 2);
double total = (sum1 + sum2 + sum3 + sum4 + sum5 + sum6 + sum7 + sum8 + sum9 + sum10);
double squaredVariance = (total)/10;
double newTotal = Math.sqrt(squaredVariance);
System.out.printf("Standard deviation is: ");
System.out.printf("%.2f", newTotal);
}
}
여기 저기 난처한 곳에서 배열을 사용할 수없는 부분을 언급하는 것을 잊었습니다. – Elias
제한 사항은 무엇입니까? 금지 된 배열일까요? – phflack
배열과리스트 모두 허용되지 않습니다. – Elias