samedi 16 décembre 2017

Java cant get if statment to work in 2d array

I have a project where i have to use a 2d array to get the grades of 5 students. I have the grid layed out fine and i can get the highest average of all of them. but i cant seem to get the lowest average outputed correctly. it seems that in my if statment it goes through each column to see if its below 50. but i just want it to take the sum like i had before. thanks in advance

public static void main(String[] args) {

    grade = new String[5][6];
    // Set the values for the first row and column 
    //row 1 
    grade[0][0]="Students "; 
    grade[0][1]="Grade 1 "; 
    grade[0][2]="Grade 2 ";  
    grade[0][3]="Grade 3 ";  
    grade[0][4]="Grade 4 ";  
    grade[0][5]="Grade 5 ";
    //Column 1 
    grade[0][0]="Students"; 
    grade[1][0]="Ryan Miller"; 
    grade[2][0]="Timothy LoPapa";  
    grade[3][0]="Hashim Razak";  
    grade[4][0]="Evan Vanderheide";  

    //call methods 
    populateGradesArray();
  displayGrades();
  getAverage();


}



   //populate grades to the board start at row 1 and row 2 to stop the constants from being written over
   for(int row = 1; row< grade.length; row++){
        for(int col=1; col<grade[row].length; col++){
            int rand = rng.nextInt(90)+10   ; // have a inital grade of 0-90 then add 10 to avoid getting grades that are unlikely to below but still creating possiblity of high grades 
            String randi = Integer.toString(rand);
            grade[row][col]=randi;
        }
    }
}

//Method to display grades to console
public static void displayGrades(){
   //print out grades to board
    for(int row =0; row< grade.length; row++){
        for(int col=0; col<grade[row].length; col++){
           grade[row][col] = grade[row][col];

           if(col <1)
           System.out.printf("%-18s",grade[row][col]);// Have the first column have enoughspacing for large names 
           else
            System.out.printf("%-10s",grade[row][col]);  //keep everything after first column from having to large of a spacing  

        }

        System.out.println(" ");

    }

}



public static void getAverage(){
    int col = 0; 
    double ave= 0; 
    double ave2= 0;
    double ave3= 0;
    double ave4= 0;
    double badave4= 0;
    double highestAve =0;   
    String studentName =  null; 

         for(int row=1; row<grade[col].length; row++){

       // get the Double vaulue of each row 
             ave+= Double.valueOf(grade[1][row])/5; 
             ave2+= Double.valueOf(grade[2][row])/5; 
             ave3+= Double.valueOf(grade[3][row])/5; 
             ave4+= Double.valueOf(grade[4][row])/5; 




             //Sort out the highest value of each row 
              highestAve = Math.max( ave, Math.max( ave2, Math.max( ave3, ave4 ) ) );

             // Assign names to each row average 
            if(highestAve == ave)
                 studentName = grade[1][0];    
            else if(highestAve == ave2)
                 studentName = grade[2][0]; 
            else if(highestAve == ave3)
                 studentName = grade[3][0]; 
            else if(highestAve == ave4)
                 studentName = grade[4][0]; 

         if(ave < 50)
               System.out.printf("%n%s is struggling with an average of %.2f",grade[1][0], ave);
         else 
             break; 
         if(ave2 < 50)
               System.out.printf("%n%s is struggling with an average of %.2f",grade[2][0], ave2);
         else 
             break; 
           if(ave3 < 50)
               System.out.printf("%n%s is struggling with an average of %.2f",grade[3][0], ave3);
           else
               break;
          if(ave4 < 50)
               System.out.printf("%n%s is struggling with an average of %.2f",grade[4][0], ave4);
          else
              break;

}

System.out.printf("%nThe student with the highest Average of %.2f is %s", highestAve, studentName);

}

}

Aucun commentaire:

Enregistrer un commentaire