I'm new to Java and at a loss what I'm doing wrong. I'm just trying to save the highscore and then print it.
int max = players[0].getHighScore();
int bestIndex = 0;
String bestName = "testing";
for (int i = 1; i < players.length; i=i+1) {
if (players[i].getHighScore() > max) {
bestIndex = i;
max = players[i].getHighScore();
bestName = players[i].getName();
}
}
System.out.println(bestIndex);
System.out.println(max);
System.out.println(bestName);
The variable max will update for every loop and then print the high score, no problems there. But the two other variables, bestIndex and bestName will just stay with the same value as initiated. I've even tried setting bestIndex and bestName to a constant and a string respectively inside the if statement but they won't change. If I remove the if statement, both will change, but ofcourse then I will just end up with the last entries, rather than the ones corresponding to the high score. So I figure the problem is with the if statement but other than that, I have no clue.
Aucun commentaire:
Enregistrer un commentaire