[英]Simple Java App to calculate the average of an indefinite amount of entered numbers
I am completely new to Java and have an assignment coming up; 我是Java的新手,并且有工作要做。 with the brief being the following:
简要说明如下:
A Swing interface. Swing接口。 The application must allow the user to enter some data and click a button.
该应用程序必须允许用户输入一些数据并单击一个按钮。 The application must have an event handler to react to the click button event.
该应用程序必须具有事件处理程序,以对单击按钮事件做出反应。 The application must perform some operation on the data entered.
应用程序必须对输入的数据执行某些操作。 The application must return the modified data to the user.
应用程序必须将修改后的数据返回给用户。
I was originally going to create an app that converted numbers to different units (eg. kgs to lbs) but found it very difficult, so have decided to go for an app which finds the average of a number of inputs. 我本来打算创建一个将数字转换为不同单位(例如,公斤到磅)的应用程序,但是发现它非常困难,因此决定选择一款能够找到多个输入平均值的应用程序。
This is the code i have for finding the average of a pre-defined array: 这是我用于查找预定义数组平均值的代码:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
class FindAverageNumber extends JFrame {
public static void main(String[] args){
double numbers[] = new double[]{1,2,3,4,5,6,7,8,9,20};
double average = 0;
double cumulative = 0;
for (int i=0; i<numbers.length; i++){
double selected = numbers[i];
cumulative = (cumulative + selected);
}
average = (cumulative / numbers.length);
System.out.println("The average of the array is: "+average);
}
}
The code seems to function properly but, being new to Java, i don't know how to take in these numbers from the user while also incorporating a swing interface (which i'm lost on). 该代码似乎可以正常运行,但是,对于Java来说,这是新手,我不知道如何从用户那里获取这些数字,同时还包含一个摆动接口(我迷路了)。 I'm assuming accepting an array is the best way to do this, with the user being presented with a textfield.
我假设接受数组是执行此操作的最佳方法,并且向用户显示一个文本字段。 I don't know how to separate the inputs up into their own index in the array.
我不知道如何将输入分成数组中自己的索引。
I'd appreciate any help; 我将不胜感激。 i realise i have a lot of basic questions.
我意识到我有很多基本问题。
If you have a text field, you'll need to parse the input. 如果您有文本字段,则需要解析输入。 You can use the
String.split()
method: 您可以使用
String.split()
方法:
For example, if you received the input
as "1,2,3,4,5"
you would call it like 例如,如果您收到的
input
为"1,2,3,4,5"
,则将其命名为
input.split(",");
Instead of trying to accept an array, it would go for a UI containing a JTextField
for input of a single number and a JButton
to submit the input. 与其尝试接受一个数组,不如选择一个UI,该UI包含一个用于输入单个数字的
JTextField
和一个用于提交输入的JButton
。 You can then update the average/total/... (whatever you want to calculate) based on the numbers you already processed. 然后,您可以根据已处理的数字更新平均值/总计/ ...(无论要计算什么)。 If the user wants to add an extra number, it just fills in an extra value and presses the button again.
如果用户想添加一个额外的数字,则只需填写一个额外的值,然后再次按下按钮。 Much more friendly to the user then having to input an array, and less possibilities to get confused (separate numbers by spaces, semicolons, comma's, ... )
这样一来,用户不得不输入数组,对用户更加友好,并且混淆的可能性也越来越小(数字之间用空格,分号,逗号隔开,...)。
To display the calculated values you can use a JLabel
(or multiple ones). 要显示计算值,可以使用
JLabel
(或多个)。
To get you started on UI building you can take a look at the excellent Swing tutorial . 为了让您开始构建UI,您可以看一下出色的Swing教程 。 The keywords to search for or the classes I already mentioned, and
JFrame
and JPanel
as container to add all those components to 要搜索的关键字或我已经提到的类,并将
JFrame
和JPanel
作为容器将所有这些组件添加到
You can take the user input as a string delimited by some character like ,(comma) or :(colon). 您可以将用户输入作为字符串,以诸如,(逗号)或:(冒号)之类的字符分隔。 Once you have the input you can split in numbers and do the computation.
输入后,您可以分割数字并进行计算。
Note: You being a student, I don't want to give the exact program to do this. 注意:您是学生,我不想提供确切的程序来执行此操作。 Explore on your own and if again you have some difficulty you can ask questions.
自行探索,如果再次遇到困难,可以提出问题。
Have a look at any Swing tutorial. 看看任何Swing教程。 Specifically, look at
JFrame
, JTextField
, JButton
. 具体来说,请看
JFrame
, JTextField
和JButton
。 You should also read up on what MVCs are. 您还应该阅读什么是MVC。 In this case, your model consists of the numbers entered (or some function there-of).
在这种情况下,您的模型由输入的数字(或其中的某些功能)组成。 So you can choose to avoid comma separated strings and just expose an Add button.
因此,您可以选择避免使用逗号分隔的字符串,而只显示“添加”按钮。 If you do go the route of allowing only one number to be entered at a time, look at
JFormattedTextField
paired with a DecimalFormat
如果您确实允许一次只输入一个数字,请查看
JFormattedTextField
与DecimalFormat
配对
Also, from a mathematical point of view, if you are keeping track of an indefinite amount of numbers, you should consider using BigDecimal
. 另外,从数学角度来看,如果要跟踪数量不确定的数字,则应考虑使用
BigDecimal
。 Note that it has a constructor that takes a string. 请注意,它具有采用字符串的构造函数。
Go google "Java Swing Tutorial." 转到Google“ Java Swing教程”。 There will be several options to choose from on how to display UI controls the user.
关于如何显示用户界面控件,将有多个选项可供选择。
http://www.javabeginner.com/java-swing/java-swing-tutorial http://www.javabeginner.com/java-swing/java-swing-tutorial
There is even code for showing a JTextField on a JFrame in there. 甚至还有用于在其中显示JFrame上的JTextField的代码。 You should be able to figure out how to add a JButton to that mix as well.
您应该能够弄清楚如何向该混合添加JButton。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.