简体   繁体   中英

JAVA: Trying to update variables inside a for loop with statement. Only one variable gets updated

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.

I ran your code and it works fine. Your player class might be the fault. Here is my demo:

public class Demo {
    public static void main(String[] args) {
       Demo test = new Demo();
        test.test();
    }

    public void test(){
        Player[] players = new Player[5];
        players[0] = new Player("aa", 10);
        players[1] = new Player("bb", 11);
        players[2] = new Player("cc", 12);
        players[3] = new Player("dd", 13);
        players[4] = new Player("ee", 14);

        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: " + bestIndex);
    System.out.println("max: " + max);
    System.out.println("bestName: " + bestName);
    }

    private class Player{
        private String name;
        private int  highScore;

        Player(String name, int highScore) {
            this.name = name;
            this.highScore = highScore;
        }


        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getHighScore() {
            return highScore;
        }

        public void setHighScore(int highScore) {
            this.highScore = highScore;
        }
    }
}

Here is the output:
bestIndex: 4
max: 14
bestName: ee

I guess the array initialization is incorrect. Use this lines for initializing and run your app:

    Player player1 = new Player();
    player1.setName("John");
    player1.setHighScore(3);

    Player player2 = new Player();
    player2.setName("Michel");
    player2.setHighScore(5);

    Player player3 = new Player();
    player3.setName("Mike");
    player3.setHighScore(9);

    Player[] players= new Player[]{player1,player2,player3};

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