简体   繁体   English

从JTextField获取数字作为数组用于Java中的标准偏差计算

[英]Get Numbers from JTextField as Array for Standard Deviation Calculation in Java

I'm new to Java programming and right now I'm making a Calculation app where you can input numbers to one textfield and get the result of Mean, Median, Mode and Standard Deviation. 我是Java编程的新手,现在正在制作一个Calculation应用程序,您可以在其中将数字输入一个文本字段,并获取Mean,Median,Mode和Standard Deviation的结果。 the problem I'm facing right now is in Standard Deviation part. 我现在面临的问题是“标准偏差”部分。 I've created a method to calculate the Standard Deviation based on the numbers inputted on the one textfield in app using the code below: 我使用以下代码创建了一种方法,用于根据在应用程序中一个文本字段上输入的数字来计算标准偏差:

static double Q2(ArrayList<Integer> input) {
    Collections.sort(input);

    ArrayList<Double> input2 = new ArrayList<>();
    double sum = 0;
    double sum2 = 0;
    double sd = 0;

    for (int i = 0; i < input.size(); i++) {
        sum = sum + input.get(i);
    }

    double mean = sum / input.size();

    for (int i = 0; i < input.size(); i++) {
        input2.add((Math.pow((input.get(i) - mean), 2)));
    }

    for (int i = 0; i < input2.size(); i++) {
        sum2 = sum2 + input2.get(i);
    }

    double mean2 = sum2 / input2.size();

    sd = Math.sqrt(mean2);

    return sd;
}

and this is my actionListener code: 这是我的actionListener代码:

standardevButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String [] numString = inputField.getText().trim().split(" ");
            outputLabel.setText(getStandarDev(getValueSet(numString)));
        }

but the problem I have now is I don't know how to get the numbers inputted in the textfield and show the result of Standard Deviation calculation when I press the Standard Deviation button in app. 但是我现在遇到的问题是,当我按应用程序中的“标准偏差”按钮时,我不知道如何获取在文本字段中输入的数字并显示标准偏差计算的结果。

This is the interface of the application 这是应用程序的界面

below is the code for main method and Standard Deviation: 以下是主要方法和标准偏差的代码:

public static void main(String [] args)
{
    TugasMatematika runIt = new TugasMatematika();
}
public static String getStandarDev(double [] nums) 
{

}

I've been searching for the answers for this problem quite a while so I'll be really really grateful if someone can help me to solve this problem. 我一直在寻找这个问题的答案,所以如果有人可以帮助我解决这个问题,我将非常感激。

在标准偏差按钮上使用鼠标单击侦听器,然后使用set方法将文本字段设置为所需答案的文本

This line of code here: 这行代码在这里:

String [] numString = inputField.getText().trim().split(" ");

Actually gets every space seperated numbers on your textfield. 实际上在您的文本字段上获取每个空格分隔的数字。 However, these data are in string data type so you would have to parse them one by one to a numerical data type such a double or integer. 但是,这些数据是字符串数据类型,因此您必须将它们一一解析为数值数据类型,例如双精度或整数。 Let me give you an idea here: 让我在这里给您一个想法:

for each String variable in my String array
receivingIntegerVariable = Integer.parseInt(stringVariable) or
receivingDoubleVariable = Double.parseDouble(stringVariable)

On the other hand, if you would like to display the actual result of computation after button click, try to attach an event listener to the button and have a handling method that would update your JTextField with the result you had with your computation. 另一方面,如果您想在单击按钮后显示实际的计算结果,请尝试将事件侦听器附加到按钮上,并具有一种处理方法,该处理方法将根据您的计算结果来更新JTextField。

JTextFieldX.setText(resultGoesHere);

EDIT: It seems like you're using a label for display your results. 编辑:似乎您正在使用标签显示结果。 The same concept applies in here. 同样的概念在这里适用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM