Hi all...I have just started the chapter on Arrays today and I'm a bit spun. Fistly My teach expects everything that can be done in JOptionPane to be done that way.
Here is my assignment code so you can see how bad off I am.
import javax.swing.JOptionPane; import java.util.*; public class SmallestElement { static Scanner console = new Scanner(System.in); public static void main(String[] args) { double[] list = new double[10]; double sum, biggestnumber, smallestnumber; int index; //Store user input JOptionPane.showMessageDialog(null, "Enter ten integers: "); list[0] = Integer.parseInt(JOptionPane.showInputDialog(null, "1st number" + " ")); list[1] = Integer.parseInt(JOptionPane.showInputDialog(null, "2nd number" + " ")); list[2] = Integer.parseInt(JOptionPane.showInputDialog(null, "3rd number" + " ")); list[3] = Integer.parseInt(JOptionPane.showInputDialog(null, "4th number" + " ")); list[4] = Integer.parseInt(JOptionPane.showInputDialog(null, "5th number" + " ")); list[5] = Integer.parseInt(JOptionPane.showInputDialog(null, "6th number" + " ")); list[6] = Integer.parseInt(JOptionPane.showInputDialog(null, "7th number" + " ")); list[7] = Integer.parseInt(JOptionPane.showInputDialog(null, "8th number" + " ")); list[8] = Integer.parseInt(JOptionPane.showInputDialog(null, "9th number" + " ")); list[9] = Integer.parseInt(JOptionPane.showInputDialog(null, "10th number" + " ")); //Sum the input sum = list[0] + list[1] + list[2] + list[3] + list[4] + list[5] + list[6] + list[7] + list[8] + list[9]; JOptionPane.showMessageDialog(null, "The sum of the numbers = " + sum); //Print in reverse order JOptionPane.showMessageDialog(null, "The numbers in reverse order are: "); JOptionPane.showMessageDialog(null, list[9] + " " + list[8] + " " + list[7] + " " + list[6] + " " + list[5] + " " + list[4] + " " + list[3] + " " + list[2] + " " + list[1] + " " + list[0]); //Find the largest Number in the array int maxIndex = 0; for (index = 1; index < list.length; index++) { if (list[maxIndex] < list[index]) maxIndex = index; } biggestnumber = list[maxIndex]; JOptionPane.showMessageDialog(null, "The largest number you entered was: " + biggestnumber); //Find the Smallest Number in the array int minIndex = 0; for (index = 0; index < list.length; index++) if (list[minIndex] > list[index]) minIndex = index; smallestnumber = list[minIndex]; JOptionPane.showMessageDialog(null, "The smallest number you entered was: " + smallestnumber); } }
What I need is to create this program with as few a function handled by the main method and use a heading called
for example...public static min(double[] array)
Any advice would be great.
Neil