运行结果:
主要公式:
1.正态分布的计算公式
2.标准正态分布的计算公式(本题使用)
源码:
import javax.swing.*;
import javax.swing.plaf.FontUIResource;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Enumeration;
import java.util.Random;
public class Final {
public static void main(String[] args) {
InitGlobalFont(new Font("宋体", Font.PLAIN, 20));
int gap = 10;
JFrame f = new JFrame("正态分布生成器");
f.setSize(800, 600);
f.setLocation(200, 200);
f.setLayout(null);
JPanel pInput = new JPanel();
pInput.setBounds(gap, gap, 300, 300);//输入框大小
pInput.setLayout(new GridLayout(6,1,gap,gap));
JLabel avg = new JLabel("均值:");
JTextField avgText = new JTextField();
JLabel stdDev = new JLabel("标准差:");
JTextField stdDevText = new JTextField();
JLabel dataNum = new JLabel("例数:");
JTextField dataNumText = new JTextField();
JLabel numPoint = new JLabel("小数点后位数:");
JTextField numPointText = new JTextField();
JLabel min = new JLabel("最小值:");
JTextField minText = new JTextField();
JLabel max = new JLabel("最大值:");
JTextField maxText = new JTextField();
JButton b = new JButton("生成");
JButton b1 = new JButton("清除");
pInput.add(avg);
pInput.add(avgText);
pInput.add(stdDev);
pInput.add(stdDevText);
pInput.add(dataNum);
pInput.add(dataNumText);
pInput.add(numPoint);
pInput.add(numPointText);
pInput.add(min);
pInput.add(minText);
pInput.add(max);
pInput.add(maxText);
pInput.add(b);
pInput.add(b1);
//文本域
TextArea ta = new TextArea();
TextArea ta1 = new TextArea();
ta.setBounds(350, 10, 330, 300);//输出框位置
ta1.setBounds(350, 320, 330, 200);//输出框位置
b.setBounds(100, 350, 100, 40);//按钮位置
b1.setBounds(200, 350, 100, 40);//检测按钮位置
f.add(pInput);
f.add(b);
f.add(b1);
f.add(ta);
f.add(ta1);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
//鼠标监听
b.addActionListener(new ActionListener(){
boolean checkedpass = true;
public void actionPerformed(ActionEvent e){
checkedpass = true;
checkEmpty(avgText,"均值");
checkEmpty(stdDevText,"标准差");
checkNumber(dataNumText,"例数");
checkNumber(numPointText,"小数点后位数");
checkEmpty(minText,"最小值");
checkEmpty(maxText,"最大值");
double avg = Double.parseDouble(avgText.getText());
double stdDev = Double.parseDouble(stdDevText.getText());
double min = Double.parseDouble(minText.getText());
double max = Double.parseDouble(maxText.getText());
int dataNum = Integer.parseInt(dataNumText.getText());
int numPoint = Integer.parseInt(numPointText.getText());
if(checkedpass){
String []arr = NormalDistribution(avg,stdDev,min,max,dataNum,numPoint);
double []num = new double[arr.length];
ta.setText("");
ta1.setText("");
for (int i = 0; i < arr.length; i++) {
ta.append(arr[i]);
ta.append("\n");
num[i] = Double.parseDouble(arr[i]);
}
ta1.append(test(num));
}
}
//检验是否为空
private void checkEmpty(JTextField tf, String msg){
if(!checkedpass)
return;
String value = tf.getText();
if(value.length()==0){
JOptionPane.showMessageDialog(f, msg + " 不能为空");
tf.grabFocus();
checkedpass = false;
}
}
//检验是整数
private void checkNumber(JTextField tf, String msg){
if(!checkedpass)
return;
String value = tf.getText();
try {
Integer.parseInt(value);
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(f, msg + " 必须是整数");
tf.grabFocus();
checkedpass = false;
}
}
});
b1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ta.setText("清除成功!");
ta1.setText("清除成功!");
}
});
}
class ButtonListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
}
}
/**
* 统一设置字体,父界面设置之后,所有由父界面进入的子界面都不需要再次设置字体
*/
private static void InitGlobalFont(Font font) {
FontUIResource fontRes = new FontUIResource(font);
for (Enumeration<Object> keys = UIManager.getDefaults().keys(); keys.hasMoreElements();) {
Object key = keys.nextElement();
Object value = UIManager.get(key);
if (value instanceof FontUIResource) {
UIManager.put(key, fontRes);
}
}
}
/*
* 根据6个参数生成随机数
**/
public static String[] NormalDistribution(double avg,double stdDev,double min,double max,int dataNum,int numPoint){
Random random = new Random();
int index = 0;
String []arr = new String[dataNum];
while(true){
double number = (stdDev*random.nextGaussian()+avg);
if(number<=max&&number>=min){
arr[index] = String.format("%."+numPoint+"f",number);
index++;
if(index==dataNum){
return arr;
}
}
}
}
public static String test(double[] arr){
/*检查这个数组的最大值,最小值,平均值,标准差,条数,小数点位数*/
double min = arr[0];
double max = arr[0];
double sum = 0;
for (int i = 0; i < arr.length; i++) {
if(arr[i]<min){
min = arr[i];
}
if(arr[i]>max){
max = arr[i];
}
sum += arr[i];/*计算数组数据之和*/
}
int dataNum = arr.length;/*数据条数就是数组的长度*/
double avg = sum/dataNum;/*计算数组均值*/
double dVar = 0;
for (int i = 0; i < arr.length; i++) {
dVar+= (arr[i]-avg)*(arr[i]-avg);
}
double stdDev = Math.sqrt(dVar/dataNum);
return "均值:"+avg+
"\n标准差:"+stdDev+
"\n例数:"+dataNum+
"\n最小值:"+min+
"\n最大值:"+max;
}
}