-2

首先,我知道如何找到均值。所以现在我的程序描述是 - 我使用方法名称nextGaussian来获取正态分布的数字,这个方法工作正常。我还使用了CONSOLE SCREEN来检查正态分布的数字曲线。当我试图找到平均值(平均值)之后,它给了我错误,

1.例如假设我们有五个数字(30,50,60,70,80)

  • 首先,我需要找到它们的总和,然后将它们除以 5,但是在我的程序中,它取最后一个数字并将这个数字除以 5,这意味着它取 80,然后将这个数字除以 5

2.如果您需要任何其他信息,请发表评论,我会这样做...

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
import java.text.NumberFormat;
import java.util.Arrays;
import java.util.Random;;
public class NormalNumberProblem extends JFrame 
{
    static JTextField a = new JTextField();
    static JButton x = new JButton();

  public static void main(String args[]) 
  {
    // construct frame
    new NormalNumberProblem().show();
  }
  public NormalNumberProblem() 
  {
    // frame constructor
    setTitle("NormalNumberProblem");

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    getContentPane().setLayout(new GridBagLayout());

    GridBagConstraints gridConstraints = new GridBagConstraints();
    a.setText("");
    a.setColumns(15);
    gridConstraints.gridx = 0;
    gridConstraints.gridy = 1;
    getContentPane().add(a, gridConstraints);

    x.setText("Random Numbers");
    gridConstraints.gridx =0;
    gridConstraints.gridy =3;
    getContentPane().add(x, gridConstraints);
    x.addActionListener(new ActionListener() 
        {
            public void actionPerformed(ActionEvent e) 
                {
                Random r = new Random();
                double[] list = new double[15];
                double mean = 10, std = 50;

                for(int i = 0; i<list.length; i++)
                {
                    double sum = 0;;
                    list[i] = r.nextGaussian() * 10 + 50;

                    sum += sum + list[i];
                    System.out.println(sum); 
                    double average = sum / list.length;
                    a.setText(" The mean is " + NumberFormat.getNumberInstance().format(average));   
                }
        }

        });

    pack();
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    setBounds((int) (0.5 * (screenSize.width - getWidth())), (int) (0.5 * (screenSize.height - getHeight())), getWidth(), getHeight());
  }


}

谢谢....

4

2 回答 2

2

您需要按如下方式更改循环:

            double sum = 0;
            for(int i = 0; i<list.length; i++)
            {
                list[i] = r.nextGaussian() * 10 + 50;
                sum += list[i];
                System.out.println(sum); 
            }
            double average = sum / list.length;
            a.setText(" The mean is " + NumberFormat.getNumberInstance().format(average));   

在这里,我已经移出循环,sumaverage修复了一个错误sum += sum + list[i](你不需要同时使用+=and sum +)。

于 2013-04-27T06:05:08.373 回答
2

您需要将一些元素移出循环(否则您需要重新定义sum每次迭代,最后它只是最后一个数字):

double sum = 0;
double average = 0;

for(int i = 0; i < list.length; i++)
                {
                    list[i] = r.nextGaussian() * 10 + 50;
                    sum = sum + list[i];
                    System.out.println(sum); 
                }

average = sum / list.length;
a.setText(" The mean is " + NumberFormat.getNumberInstance().format(average));  
于 2013-04-27T06:06:42.590 回答