简体   繁体   中英

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; with the brief being the following:

A Swing interface. 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). 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:

For example, if you received the input as "1,2,3,4,5" you would call it like

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. 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).

To get you started on UI building you can take a look at the excellent Swing tutorial . The keywords to search for or the classes I already mentioned, and JFrame and JPanel as container to add all those components to

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. Specifically, look at JFrame , JTextField , JButton . You should also read up on what MVCs are. 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

Also, from a mathematical point of view, if you are keeping track of an indefinite amount of numbers, you should consider using BigDecimal . Note that it has a constructor that takes a string.

Go google "Java Swing Tutorial." 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

There is even code for showing a JTextField on a JFrame in there. You should be able to figure out how to add a JButton to that mix as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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