Search code examples
javaarraysindexingmaxmin

Index match max/min scores (Java)


I am working through a coding assignment and am close to completion save for my final print statement which needs to match names w/ scores for max and min.

I have been able to get appropriate values in two sentences using two if statements, but I am a bit stumped on how to get my indexing correct to align names to scores w/ max and min.

I am not able to use classes or additional / different methods other than arrays and indexing.

    //Create method StudentMax
private static int StudentMax(int[] Scores) {
    int ScoreMax = Scores[0];
    for (int i = 0; i < Scores.length; i++){
        if (Scores[i] > ScoreMax){
            ScoreMax = Scores[i];
        }
    }
    return ScoreMax;
}

//Create method StudentMin
private static int StudentMin(int[] Scores) {
    int ScoreMin = Scores[0];
    for (int i = 0; i < Scores.length; i++){
        if (Scores[i] < ScoreMin) {
            ScoreMin = Scores[i];
        }
    }
    return ScoreMin;
}

public static void main(String[] args) {

    //Call Scanner
    Scanner scan = new Scanner(System.in);

    //User Welcome
    System.out.println("Welcome to the student score sorter.");
    System.out.println("\nThis program will accept a number of student names and score values, then find the highest and lowest score.");
    System.out.println("\nThen will return the names and max/min scores.");


    //User Prompt Enter number of students
    System.out.println("\nHow many students would you like to enter: ");
    int StudentCount = scan.nextInt();

    //Create arrays: Scores, StudentFirst, StudentLast
    int [] Scores = new int[StudentCount];
    String [] StudentFirst = new String [StudentCount];
    String [] StudentLast = new String [StudentCount];


    for (int i = 0; i < Scores.length; i++) {
        System.out.println("\nStudent " + (i+1)+":");
        System.out.println("\nEnter Student's name:");
        StudentFirst[i] = scan.next();
        StudentLast[i] = scan.next();
        System.out.println("\nEnter Student's score (0-100):");
        Scores[i] = scan.nextInt();
    }
    

    int max = StudentMax(Scores);
    int min = StudentMin(Scores);
    


    for (int i = 0; i < Scores.length; i++) {
        System.out.println("\n"+StudentFirst[i] + " " + StudentLast[i] +":      " + Scores[i]); 
    }
    
    
    for (int i = 0; i < Scores.length; i++)  {
        if (Scores [i] == max) {
            System.out.println("\n"+ StudentFirst[i] +" "+ StudentLast[i] + " has the highest score => " +max+ " and " + StudentFirst[i]+" " + StudentLast[i]+ " has the lowest => " +min);         
        }
    }
    
    
    
    //This is the sentence format that I need to make work, but I am struggling to understand how to align the index for names and scores. 
    //System.out.println("\n"+StudentFirst[i] +" "+ StudentLast[i]+ " has the highest score => " +max+ " and " +StudentFirst[i] +" "+ StudentLast [i]+ " has the lowest score => " +min);
    
    
     
    
    


    //Scan Close
    scan.close();
    //Close Program
}

}


Solution

  • Pass back the index not the value

    private static int StudentMin(int[] Scores) {
        int ScoreMin = Scores[0];
        int index = 0;
        for (int i = 0; i < Scores.length; i++){
            if (Scores[i] < ScoreMin) {
                ScoreMin = Scores[i];
                index = i;
            }
        }
        return index;
    }
    

    Then you can use it later

    int index = StudentMax(Scores);
    
    System.out.println("\n"+ StudentFirst[index] +" "+ StudentLast[index] + " has the highest score => " +Scored[index]);  
    

    Note Please pay attention to Java naming conventions