Closed. This question does not meet Stack Overflow guidelines。它当前不接受答案。
                        
                    
                
            
        
            
        
                
                    
                
            
                
                    想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
                
                    2年前关闭。
            
        

    

我必须编写一个程序,该程序读取一组浮点值(总计10个。它是固定的),然后计算并显示值的平均值,标准偏差,最小的值,最大的值,第二个最大的价值
它应该有一个循环。在循环中,提示用户输入数字(可以是小数部分的浮点数),然后将数字保存在变量中(双精度型)。我已经做到了,但是我在实现一个循环以使输入值更容易时遇到了麻烦,因为在通过循环计算值时,我不知道如何保存它们。所以现在我的代码看起来非常丑陋和多余。

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);


    }

}

最佳答案

不用命名每个变量,而是尝试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 min = Arrays.stream(inputs).min().getAsDouble();

关于java - 标准偏差Java,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47624160/

10-10 05:53