【问题标题】:JAVA: Trying to update variables inside a for loop with statement. Only one variable gets updatedJAVA:尝试使用语句更新 for 循环内的变量。只有一个变量被更新
【发布时间】:2023-04-01 17:33:02
【问题描述】:

我是 Java 新手,不知道自己做错了什么。我只是想保存高分然后打印出来。

    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);

变量 max 将为每个循环更新,然后打印高分,没有问题。但是另外两个变量 bestIndex 和 bestName 将保持与启动时相同的值。我什至尝试在 if 语句中分别将 bestIndex 和 bestName 设置为常量和字符串,但它们不会改变。如果我删除 if 语句,两者都会改变,但当然我只会得到最后的条目,而不是对应于高分的条目。所以我认为问题出在 if 语句上,但除此之外,我没有任何线索。

【问题讨论】:

  • bestName 应初始化为 players[0].getName(), FWIW。
  • 您的players 包含什么内容?你说结果没有更新,但是代码看起来很好,除了 Qix 提到的初始化。
  • 你发誓代码看起来像这样吗,米歇尔?因为这应该工作......
  • players 是一个对象数组,如果我的措辞正确的话。但是 max = player[i].getHighScore();工作得很好,if 语句中的其他两行是不工作的。
  • players[0] 得分最高,不是吗?

标签: java loops if-statement


【解决方案1】:

我运行了您的代码,它运行良好。你的玩家等级可能是错的。这是我的演示:

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;
        }
    }
}

这是输出:
bestIndex:4
最大:14
bestName: ee

【讨论】:

    【解决方案2】:

    我猜数组初始化不正确。使用此行来初始化和运行您的应用程序:

        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};
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-01-02
      • 1970-01-01
      • 2021-12-17
      • 1970-01-01
      • 1970-01-01
      • 2021-06-29
      • 1970-01-01
      相关资源
      最近更新 更多