View Single Post
  #5 (permalink)  
Old 03-26-2008, 09:34 PM
dc2acgsr99's Avatar
dc2acgsr99 dc2acgsr99 is offline
Member
 
Join Date: Jan 2008
Location: Pittsburgh
Posts: 26
dc2acgsr99 is on a distinguished road
Send a message via AIM to dc2acgsr99
Its been a while since I been here, just started a job and haven't had much time to work on my Java class work between work and other classes. Anyways below is my code as it stands but my problem is with the average number for the project. For some reason it is not showing the average of the numbers I input.. I am at a complete loss here. So here is the code, a second set of eyes would be great, Thanks....

Code:
import java.util.Arrays; import javax.swing.*; public class Lab3 { public static void main(String[] args) //start main { program(); System.exit(0); } //end main public static void program() //start program { intro(); int numberOfStudents = 0; String response = null; do { response = JOptionPane.showInputDialog("Enter number of students."); numberOfStudents = Integer.parseInt(response); } while(response == null || response.length() == 0); String[] names = new String[numberOfStudents]; int[] scores = new int[numberOfStudents]; getInput(names, scores); System.out.printf("names = %s%n", Arrays.toString(names)); System.out.printf("scores = %s%n", Arrays.toString(scores)); // get max int myMax; myMax = max (scores); System.out.println ("MyMax is: " + myMax); // get min int myMin; myMin = min (scores); System.out.println ("MyMin is: " + myMin); // get average int myAvg; myAvg = doAvg (scores); System.out.println ("MyAvg is: " + myAvg); } //end program public static void intro() //start msg { JOptionPane.showMessageDialog(null, "This program will take " + "a given number of students and show you the user their scores "+ "from highest to lowest with the class average."); } //end msg public static void getInput(String[] names, int[] scores) //start info grab { // Fill the arrays with user input. int count = 0; do { String s = JOptionPane.showInputDialog("What is the students name?"); names[count] = s; s = JOptionPane.showInputDialog("Please enter a number."); scores[count] = Integer.parseInt(s); } while(count++ < names.length-1); } //end infor grab public static int max(int[] t) //start max num { int maximum = t[1]; for (int i = 0; i < t.length; i++) { if (t[i] > maximum) { maximum = t[i]; } } return maximum; } //end max num public static int min(int[] t) { int minimun = t[1]; // start min num for (int i = 0; i < t.length; i++) { if (t[i] < minimun) { minimun = t[i]; } } return minimun; } //end end min num public static int doAvg(int[] scores) //start avg num { int sum = 0; for (int ctr = 0; ctr < scores.length; ctr++) { sum += scores[ctr]; } return sum / (scores.length - 1); } //end avg num }
__________________
Hey I'm still learning... Thats my story and I'm sticking to it
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Reply With Quote