[java] I am kinda new to this Java stuff...

Started by
5 comments, last by XyurmomX 22 years, 4 months ago
and for some reason I can''t get this program to work like I want it too. I have to output the greatest and lowest averages. It works fine for 2 students, but when I enter more than 3 it doesnt work. Can you guys please give me some suggestions?


public class parallel {
   
   public static void main(String[] pars) {
   
      int n;
      System.out.print("Enter number of students: ");
      n= JLiveRead.readInt();

      
      
      int x = 0;
      int y = 0;
      double hold = 0;
      double hold2 = 0;
      double hold3 = 0;
      double lowest = 0;
      double greatest = 0;
      double totalaverage = 0;

      String[] name;
      int[] exam1;
      int[] exam2;
      int[] exam3;
      double[] average;

      name=  new String[16];
      exam1= new int[n];
      exam2= new int[n];
      exam3= new int[n];
      average= new double[n];
    
      
      for (int i= 0; i < n; i++) {

         System.out.println();

         System.out.print("Enter Student #" + (i + 1) + ": ");
         name[x]= JLiveRead.readLineString();         

         System.out.print("Enter Exam #1 Score: ");
         exam1[x]= JLiveRead.readLineInt();          
         
         if (exam1[x] < 0 || exam1[x] > 100) {
            System.out.print("Grade must be between 0-100.  Enter again: ");
            exam1[x]= JLiveRead.readLineInt();
         }

         System.out.print("Enter Exam #2 Score: ");
         exam2[x]= JLiveRead.readLineInt();         

         if (exam2[x] < 0 || exam2[x] > 100) {
            System.out.print("Grade must be between 0-100.  Enter again: ");
            exam2[x]= JLiveRead.readLineInt();
         }

         System.out.print("Enter Exam #3 Score: ");
         exam3[x]= JLiveRead.readLineInt();         

         if (exam3[x] < 0 || exam3[x] > 100) {
            System.out.print("Grade must be between 0-100.  Enter again: ");
            exam3[x]= JLiveRead.readLineInt();
         }

         System.out.println();

         average[x] = ( exam1[x] + exam2[x] + exam3[x] ) / 3;         

         x= x+1;
              
      }            

      System.out.println("Student Exam Average: ");
       

      for (int i= 0; i < n; i++) {

         System.out.println(name[y] + "   " + average[y]);
         y= y+1;
  
      }     


      System.out.println();

      x= 0;       

      for (int i= 0; i < n-1; i++) {
         if (average[x] < average[x+1])
            lowest= average[x]; 
         else 
            lowest= average[x+1];
         x= x+1;  
      }

      System.out.println("Students having the lowest average: (" +  (int)lowest +")");

      x= 0;

      for (int i= 0; i < n; i++) {
         if (average[x] == lowest)
            System.out.println(name[x]);
         x= x+1;
      }

      System.out.println();
      System.out.println();

      x= 0;

      for (int i= 0; i < n-1; i++) {

         if (average[x] < average[x+1])
            greatest= average[x+1];
         else 
            greatest= average[x];
         x= x+1;
         
 
      }

      System.out.println("Students having the greatest average: (" + (int)greatest  + ")");
      
      x= 0;
      for (int i= 0; i < n; i++) {
         if (average[x] == greatest)
            System.out.println(name[x]);

         x= x+1;
      }




      System.out.println();
      System.out.println();    

    x= 0;

    for (int i = 0; i < n; i++) {

       hold= exam1[x] + hold;
       x= x+1;       
    }
       

    System.out.println("Exam #" + 1 + " Class Average: " + (hold / n));
    
    x = 0;
    

    for (int i = 0; i < n; i++) {

       hold2= exam2[x] + hold2;
       x= x+1;       
    }
       

    System.out.println("Exam #" + 2 + " Class Average: " + (hold2 / n));

    x= 0;
    
    for (int i = 0; i < n; i++) {

       hold3= exam3[x] + hold3;
       x= x+1;       
    }
       

    System.out.println("Exam #" + 3 + " Class Average: " + (hold / n));

    totalaverage= (hold + hold2 + hold3 ) / (n * 3);
    System.out.print("Overall Class Exam Average: " + totalaverage);
  
   }

}

 
Advertisement
I can suggest that you do your own homework
Hey buddy, listen here. I have been working on this stupid code for way too many hours, and I am just asking if anyone has any suggestion on how to help me on the greatest and lowest test scores. I wrote that horrible code on my own, thank you very much.

Your problem lies in your comparison section. You compare avg[0] with avg[1], then you compare avg[1] with avg[2], then avg[2] with avg[3], each time storing the lower number into your ''lowest'' variable and overwriting your previous lowest number. You do a similar process with your ''highest'' comparisons.

You should write that section so that the lowest number is stored and used for comparison, rather than comparing averages to each other. This code could appear as follows:

int lowest = -1;
for (int x = 0; x < n; x++) {
if (lowest == -1 || average[x] < lowest)
lowest = average[x];
}

Hope that helps!

Michael

Your basic faulty logic is in the assumption that in one pass of the check loop either of the two numbers checked will be the lowest of all...that is definately not true....for example....4 10, 13....first 4 and 10 are checked, according to your logic....4 is the lowest...so far so good....but then you check 10 and 13....10 is the lowest between THOSE TWO, but not overall...same thing with greatest average...to solve this problem take out the else's (e.g. else lowest = average[x+1] and set lowest as 9999 (or anything larger than the highest anticipated value) and set greatest to 0 . Sorry if this doesn't make sense...I tried...

Edited by - BraveZeus on December 6, 2001 1:09:07 AM
Chris
Thank you very much guys. I knew that was what my problem was, I just had no way of going about fixing it. I always get stuck on the easier parts.

Anyway, thanks again!!!!!!
I''m doing this to test my own knowledge a bit, but wouldn''t this be a perfect example for the use of a bubble sort, then just read the highest and lowest of each stack for each exam?

BeS
It''s Da BOMB Baby!!!
. o O ~
A little nonsense now and then,
is relished by the wisest men
~ O o .
-- Willy Wonka
BeSIt's Da BOMB Baby!!!. o O ~ A little nonsense now and then,is relished by the wisest men~ O o .-- Willy Wonka

This topic is closed to new replies.

Advertisement