Note that there are some explanatory texts on larger screens.

plurals
  1. POSimple Java App to calculate the average of an indefinite amount of entered numbers
    primarykey
    data
    text
    <p>I am completely new to Java and have an assignment coming up; with the brief being the following:</p> <p><em>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.</em></p> <p>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.</p> <p>This is the code i have for finding the average of a pre-defined array:</p> <pre><code>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&lt;numbers.length; i++){ double selected = numbers[i]; cumulative = (cumulative + selected); } average = (cumulative / numbers.length); System.out.println("The average of the array is: "+average); } } </code></pre> <p>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. </p> <p>I'd appreciate any help; i realise i have a lot of basic questions.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload