Chris Pollett>Old Classes>PIC 20, Winter 2000>Hw1>Hw1 Solutions
// Program Name: MeanValue.java // // Purpose: This program takes the first command line argument which // is assumed to be an integer n and pops up n successive // dialog boxes. The average of these numbers is then // presented in a message box. // // Known Bugs/ // Comments: The only exceptional case this program checks for is the // case where there is no command line argument. import javax.swing.JOptionPane; public class MeanValue { // for this simple program all of the code is in the main method public static void main (String args[]) { String inNum; int n; double sum =0; if(args.length == 0) //handle case of no cmd line arg { System.out.println("Please supply the number of"+ " items to be averaged on the command" +" line\n"); System.exit(0); } n= Integer.parseInt(args[0]); for(int i=1; i<=n; i++) // get numbers to avg { inNum =JOptionPane.showInputDialog("Enter number"+i+":"); sum += Double.parseDouble( inNum); // sum as we go } // print the average JOptionPane.showMessageDialog(null,"The average is:"+sum/n ,"Mean", JOptionPane.PLAIN_MESSAGE); System.exit(0); } }