Java Parallel arrays on finding two matching items -


so in school, learned arrays , have project on it. make parallel arrays on students name, test average score, quiz average score, homework average score, , final average score. also, have find highest , lowest scores. have highest score figured out unless 2 students score same high school, show one. here methods :

public string gethighestscore(){     double highscore = 0;     string students = "";     for(int = 0; < totaltests; i++){         if(quarteraverages[i] > highscore){             highscore = quarteraverages[i];             students = names[i];         }     }     return students; } 

this segment of code works. needed make greater in if statement greater or equal , add name string instead of changing 1 name.

public string gethighestscore(){     double highscore = 0;     string students = "";     for(int = 0; <= totaltests; i++){         if(quarteraverages[i] >= highscore){             highscore = quarteraverages[i];             students += names[i] + " ";         }     }     return students; } 

this 1 outputs 1 student high score rather 2. question how can output multiple students same highscore.

to return multiple names, need few things:

  1. you should return list<string> instead of single string value. (of course, list have 1 element if there no ties highest score.)
  2. you need check whether ith score tied current high score , add name current list if is.
  3. when new high score detected, need clear list of names (which tied, no longer high score) , add new high score name.

something (untested) might trick:

public list<string> gethighestscore() {     double highscore = 0;     list<string> students = new arraylist<>();     for(int = 0; < totaltests; i++){         if(quarteraverages[i] >= highscore){             if (quarteraverages[i] > highscore) {                 highscore = quarteraverages[i];                 students.clear();             }             students.add(names[i]);         }     }     return students; } 

edit: since learning arrays, may not know java's list data structure. here's variation returns concatenation of names, separated delimiter:

public string gethighestscore() {     double highscore = 0;     string students = "";     string delimiter = ", ";     for(int = 0; < 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, done stringbuilder, don't know you've learned that, either.)

p.s. since method returns name(s) highest score, , not highest score itself, i'd suggest rename method accordingly.


Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -