2017-12-03 3 views
0

부동 소수점 값 집합 (총 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); 


    } 

} 

답변

1

대신 모든 변수를 명명, 그런 다음 array

float inputs = new float[10]; 
for(int i = 0; i < inputs.length; i++) 
    inputs[i] = s.nextFloat(); 

을 시도하면 배열이 있으면, 당신은뿐만 아니라

float min = inputs[0]; 
for(float f : inputs) 
    min = Math.min(f, min); 

당신이 원하는 경우 루프와 최소를 찾을 수 있습니다 당신은 또한 스트림을 사용하여 최소/최대를 찾을 수 있습니다 (double의 배열에서만 작동 할 수도 있음)

double min = Arrays.stream(inputs).min().getAsDouble(); 
+0

여기 저기 난처한 곳에서 배열을 사용할 수없는 부분을 언급하는 것을 잊었습니다. – Elias

+0

제한 사항은 무엇입니까? 금지 된 배열일까요? – phflack

+0

배열과리스트 모두 허용되지 않습니다. – Elias

0

저는 현재 CompSci와 Statistics에 있기 때문에 전체 수업을 다시 썼습니다. 그래서 나는 흥미로 웠습니다.

import java.util.Arrays; 
import java.util.Scanner; 

class Main { 

    static float[] input = new float[10]; 
    static Scanner s = new Scanner(System.in); 

    public static void main(String[] args) { 

    for(int i = 0; i < 10; i++) { 
     input[i] = s.nextFloat(); 
    } 
    Arrays.sort(input); 

    System.out.println("Largest: " + input[9]); 
    System.out.println("Smallest: " + input[0]); 
    System.out.println("Second largest: " + input[8]); 
    System.out.println("Average: " + getTotal()/10); 
    System.out.printf("Standard deviation %.2f", Math.sqrt(getTotal()/10)); 
    } 

    static public float getTotal() { 
    float total = 0; 

    for(int i = 0; i < 10; i++) { 
     total += input[i]; 
    } 

    return total; 
    } 
} 
+0

하지만 이것은 매우 좋은 해결책입니다. 그러나 배열과 목록은 언급하지 않는 것을 잊었습니다. 그래서 나는이 일을 어떻게 할 것인지 잘 모른다. – Elias