使用循环查找平均值和标准差

时间:2016-03-08 03:24:28

标签: java loops standard-deviation

我的目标是计算从用户输入中获得的10个数字的平均值标准差

我在课堂上只学习 while循环

问题: 如何将每个用户输入(在循环内)分配给单独的变量,以便稍后与平均值进行比较,以找到标准偏差?

(我没有在课堂上讲过数组。所以,我必须在不使用数组的情况下解决这个问题)

我做了一个非常冗长乏味的程序,但如果需要可以提供更多代码,我可以做while循环找到意思。

import java.util.Scanner;

public class StandardDev
{
   public static void main (String[] args)
   {
   Scanner input = new Scanner (System.in);

   double num1 = 0, num2 = 0, num3 = 0, num4 = 0, num5 = 0, num6 = 0, num7 = 0, num8 = 0, num9 = 0, num10 = 0, total = 0, mean = 0, stDev1 = 0, stDev2 = 0, sum = 0;

   System.out.println("Enter 10 numbers: ");
   num1 = input.nextDouble();
   num2 = input.nextDouble();
   num3 = input.nextDouble();
   num4 = input.nextDouble();
   num5 = input.nextDouble();
   num6 = input.nextDouble();
   num7 = input.nextDouble();
   num8 = input.nextDouble();
   num9 = input.nextDouble();
   num10 = input.nextDouble();

   sum = num1+num2+num3+num4+num5+num6+num7+num8+num9+num10;

   mean = sum / (double) 10;

   stDev1 = Math.pow(num1 - mean, 2) + Math.pow(num2 - mean, 2) + Math.pow(num3 - mean, 2) + Math.pow(num4 - mean, 2) + Math.pow(num5 - mean, 2) + 
   Math.pow(num6 - mean, 2) + Math.pow(num7 - mean, 2) + Math.pow(num8 - mean, 2) + Math.pow(num9 - mean, 2) + Math.pow(num10 - mean, 2);

   stDev2 = Math.sqrt(stDev1 / 10);

   System.out.println ("The mean is " + mean + ".");
   System.out.println ("The standard deviation is " + stDev2 + ".");
   }
}

这是我找到的意思。我的逻辑告诉我,如果我可以单独访问这些输入以从均值和平方减去,我只能找到标准偏差。不知道怎么样..

import java.util.Scanner;

public class JTillman03_45
{
   public static void main (String[] args)
   {
   Scanner input = new Scanner (System.in);

   int count = 0, total = 0;

   double mean, stDev = 0;

   System.out.println("Enter a digit: ");
   int number = input.nextInt();

   while (count < 9){
      total += number;
      count++;


   System.out.println("Enter a digit: ");
   number = input.nextInt();
   }

   mean = total / (double) count;

   System.out.println("The mean of those numbers is: " + mean); 
   System.out.println("The standard deviation of those numbers is: " + stDev); 
  }
}

5 个答案:

答案 0 :(得分:2)

你真的需要存储所有整数吗?如果没有,您可以使用以下三个值计算平均值和标准偏差:

  1. 输入了多少个数字(在您的问题中,它是固定值10)。
  2. 所有数字的总和。
  3. 数字的平方和。
  4. 哪个给出了

    double count = 10.0;   // is 10.0 for your problem
    double sum1 = 0.0;    // sum of the numbers
    double sum2 = 0.0;    // sum of the squares
    int i;
    for (i=0; i < 10; i++) {
      System.out.println("Enter 10 numbers: ");
      double n = input.nextDouble();
      sum1 += n;
      sum2 += n * n;
    }
    double average = sum1 / count;
    double variance = (count * sum2 - sum1 * sum1) / (count * count);
    double stdev = Math.sqrt(variance); 
    

答案 1 :(得分:0)

使用while循环,迭代input.nextDouble()的值作为循环的条件。只要有输入,循环就会继续。同时,保持输入数量和数字总和的计数。只需使用这2个数字,您就可以计算平均值和标准偏差。如果你看stDev1,你会发现它只是数字之和减去平均时间10(或输入数)。平均值只是总和除以输入计数。如果您需要更多解释,请告诉我。我将很快添加一些示例代码。

public class StandardDev {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        while (input.hasNext()) {
            sum += input.nextDouble();
            count++;
        }
        mean = sum / count;
        stdDev = sum - (count * mean);

        System.out.println("The mean is " + mean + ".");
        System.out.println("The standard deviation is " + stdDev + ".");
    }
}

答案 2 :(得分:0)

为了总结10个用户输入的数字,您可以仅使用一个变量和+ =运算符来跟踪总和。不需要有这么多变量,这就是循环的用途!例如,

double sum = 0;
Scanner input = new Scanner (System.in);
int counter = 1;
System.out.println("Enter 10 numbers: ");
//adds up 10 user-inputted numbers
while(counter <= 10){
     sum += input.nextDouble();
     //how the loop will end after 10th iteration
     counter++;
}
double mean = sum/10; //no need for double cast here since sum is a double

如果没有数组,使用循环很难计算标准差,因为我们需要访问各个数字才能找到标准偏差。因此,除非我们让用户使用类似于上面的while循环再次输入相同的10个数字以找到方差,否则您的解决方案实际上是唯一的方法。例如,

//reset counter
counter = 1;
double variance = 0;
System.out.println("Enter 10 numbers: ");

while(counter <= 10){
     variance += Math.pow(input.nextDouble()-mean,2);
     counter++;
}
double stdDev = Math.sqrt(variance/10);
System.out.println ("The mean is " + mean + ".");
System.out.println ("The standard deviation is " + stdDev + ".");

答案 3 :(得分:0)

这是带循环的更新代码。

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    double sum = 0;
    double sqSum = 0;

    System.out.println("Enter 10 numbers: separte by space");
    for ( int i = 0 ; i < 10 ; i++){
        double val = input.nextDouble();
        sum += val;
        sqSum += val * val;
    }

    double mean = sum / 10;
    double stdDev = sqSum / 10 - mean * mean;
    System.out.println("The mean is " + mean + ".");
    System.out.println("The standard deviation is " + stdDev + ".");
}

答案 4 :(得分:0)

如果你能学习数组,那么这些东西就很容易了。可能只需要半个小时,但这将使您的编程生活变得轻松。这是我使用数组提供十个数字的均值和SD的示例:

 import java.util.*;  
     class Test  
     {  
     public static void main (String [] args)  
       {  
          System.out.println("Enter 10 numbers to find standard deviation");  
          Scanner in = new Scanner (System.in);  
          double[] arr= new double [10];  
          double sum=0, mean=0;  
          for (inti=0; i<10; i++) //Take input in the array  
         {  
               System.out.print("Enter a number : ");  
               arr[i]=in.nextDouble();  
               sum+=arr[i]; //sum of all elements  
         }  
          mean=sum/10;  
          sum=0;  
          System.out.println("Mean : "+mean); //Display mean of all elements  
          double [] temp= new double[10];  
          for (inti=0; i<10; i++) //calculate standard deviation  
         {  
               temp[i]=Math.pow((arr[i]-mean),2);  
               sum+=temp[i];  
         }  
          mean=sum/10;  
          double deviation=Math.sqrt(mean);  
          System.out.println("Deviation : "+ deviation);  
       }  
     }