简体   繁体   中英

Java Parallel arrays on finding two matching items

So in school, we just learned arrays and we have a project on it. It is to make a parallel arrays on a students name, test average score, quiz average score, homework average score, and final average score. Also, we have to find the highest and lowest scores. I have the highest score figured out unless 2 students score the same high school, then it will only show one. Here are my methods :

public String getHighestScore(){
    double highScore = 0;
    String students = "";
    for(int i = 0; i < totalTests; i++){
        if(quarterAverages[i] > highScore){
            highScore = quarterAverages[i];
            students = names[i];
        }
    }
    return students;
}

This segment of code now works. I needed to make the greater than in the if statement a greater than or equal to and add name to the string instead of changing it to one name.

public String getHighestScore(){
    double highScore = 0;
    String students = "";
    for(int i = 0; i <= totalTests; i++){
        if(quarterAverages[i] >= highScore){
            highScore = quarterAverages[i];
            students += names[i] + " ";
        }
    }
    return students;
}

This one only outputs 1 student with the high score rather than 2. My question is how can I output multiple students with the same highscore.

To return multiple names, you need to do a few things:

  1. You should return a List<String> instead of a single String value. (Of course, the list will only have one element if there are no ties for the highest score.)
  2. You need to check whether the i th score is tied with the current high score and add the name to the current list if it is.
  3. When a new high score is detected, you need to clear the list of names (which are tied, but no longer the high score) and add the new high score name.

Something like this (untested) might do the trick:

public List<String> getHighestScore() {
    double highScore = 0;
    List<String> students = new ArrayList<>();
    for(int i = 0; i < totalTests; i++){
        if(quarterAverages[i] >= highScore){
            if (quarterAverages[i] > highScore) {
                highScore = quarterAverages[i];
                students.clear();
            }
            students.add(names[i]);
        }
    }
    return students;
}

EDIT: Since you are just learning about arrays, you may not know about Java's List data structure. Here's a variation that returns a concatenation of all the names, separated by a delimiter:

public String getHighestScore() {
    double highScore = 0;
    String students = "";
    String delimiter = ", ";
    for(int i = 0; i < totalTests; i++){
        if(quarterAverages[i] >= highScore){
            if (quarterAverages[i] > highScore) {
                highScore = quarterAverages[i];
                students = "";
            } else if (students.length() > 0) {
                students += delimiter;
            }
            students += names[i];
        }
    }
    return students;
}

(This should, of course, be done with a StringBuilder , but I don't know that you've learned about that, either.)

PS Since this method returns the name(s) with the highest score, and not the highest score itself, I'd strongly suggest that you rename the method accordingly.

Buddy, given the amount of information and code you have provided, below is what best I can suggest. Idea to send a list of students instead of string containing one name.

public ArrayList<String> getHighestScore(){
    double highScore = 0;
    ArrayList<String> students = new ArrayList<String>();
    for(int i = 0; i < totalTests; i++){
        if(quarterAverages[i] > highScore){
            highScore = quarterAverages[i];
            students.add(names[i]);
        }
    }
    return students;
}

Find the highest score first, then find the students who has the highest score.

public static String getHighestScore() {
    // First get the highest score
    double highScore = 0;
    for (int i = 0; i < quarterAverages.length; i++) {
        if (quarterAverages[i] > highScore) {
            highScore = quarterAverages[i];
        }
    }

    // Now get the students with the highest score
    String students = "";
    for (int i = 0; i < quarterAverages.length; i++) {
        if (quarterAverages[i] == highScore) {
            students += names[i] + ", ";
        }
    }
    // Substring is to remove the ", " at the end.  That's what the -2 does.
    return students.substring(0, students.length() - 2);
}

EDIT

Or you could just use one loop

public static String getHighestScore() {
    // First get the highest score
    double highScore = 0;
    String students = "";
    for (int i = 0; i < quarterAverages.length; i++) {
        if (quarterAverages[i] >= highScore) {
            if (quarterAverages[i] > highScore) {
                highScore = quarterAverages[i];
                students = "";
            }
            students += names[i] + ", ";
        }
    }
    // Substring is to remove the ", " at the end.  That's what the -2 does.        
    return students.substring(0, students.length() - 2);
}

Data

private String[] names = { "Chris", "John", "Joe" };
private double[] quarterAverages = { 100.0, 100.0, 99.0 };

Result:

Chris, John

Alright so I will try my best to help you as far as I can with the information you gave us. Considering it's a school assignement, I guess you have to return a String and aren't allowed to use Lists and so on.

Also I'm assuming the following:

  • The Array names contains the student names.
  • You want to get the names of the students with the highest scores in your String. If there are several students with the same score (which is the highest), all students should be included in that String

Let's look at your first code snippet (not my proposed solution)

public String getHighestScore(){
    double highScore = 0;
    String students = "";
    for(int i = 0; i <= totalTests; i++){
        if(quarterAverages[i] >= highScore){
            highScore = quarterAverages[i];
            students += names[i] + " ";
        }
    }
    return students;
}

So let's say that now there is a student with the quaterAverage of 5 at Index 0 and a student with the quaterAverage of 6 at the Index 1. Since you add something at the end of the String if the current quarter Average is higher, you would have the name of the student at index 0 (currently the highest score) and then the name of the student at index 1 (then the highest score). That isn't what you seem to be striiving for since you only want the names of the highest scoring people.

I would propose something like this:

public String getHighestScore(){
    double highScore = -1; //maybe the highest score is 0 
    String students = "";
    for(int i = 0; i <= totalTests; i++){
        if(quarterAverages[i] > highScore){
            highScore = quarterAverages[i];
            students = names[i];
        }
        else if(quarterAverages[i] == highScore){
            students += ", " + names[i];
        }
    }
    return students;
}

So now, each time we find a student with the same score as the current highest one, we add the name to the String. If we find a new Highest score we reset the String and go from there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM