java - (二维数组)Java错误: constant expression required

标签 java data-structures

我正在做数据结构的作业,但在二维数组方面遇到了麻烦。这是我必须做的:

“假设您正在设计一款多人游戏,有 n≥1000 名玩家, 编号为 1 到 n,在魔法森林中互动。此次的获胜者 游戏是第一个至少能遇到所有其他玩家的玩家 一次(允许有平局)。假设有一个方法 meet(i, j), 每当玩家 i 遇到玩家 j(其中 i ̸= j)时都会调用该函数, 描述一种方法来跟踪相遇的玩家以及谁是获胜者。”

由于此错误,我无法编译:

Multiplayer.java:51: error: constant expression required

对于这一行:

case meet: sb.append("1");

我是初学者,所以我非常感谢任何帮助。预先感谢您!

/* Suppose you are designing a multiplayer game that has n≥1000
   players, numbered 1 to n, interacting in an enchanted forest. The winner
   of this game is the first player who can meet all the other players at 
   least once (ties are allowed). Assuming that there is a method meet(i,
   j), which is called each time a player i meets a player j (with i ̸=
   j), describe a way to keep track of the pairs of meeting players and 
   who is the winner. */

public class Multiplayer {
    int n; // number of players
    int map[][] = new int[n][n]; // create a 2D array
    int meet = 1;
    int notMeet = 0;
    int[] count; // an array to keep the count, player wins when it reaches n

    public Multiplayer() {
        clearMap();
    } // initiate a new game

    public void clearMap() { // clear the 2d array
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                map[i][j] = notMeet; // clearing the map
                count[i] = 0; // turn every value of count[] into 0
                if (i == j)
                    map[i][j] = map[j][i] = meet; // when i == j give the tile the value of 1
            }
        }
    }

    public void meet(int i, int j) {
        // when player i meets player j, add 1 to the count[] of each player
        count[i] = count[i] + 1;
        count[j] = count[j] + 1;
    }

    public int isWin() {
        for (int i = 0; i < n; i++) {
            if (count[i] == n)
                return i; // player at the index i wins
        }
        return -1; // no player won yet
    }

    public String toString() {
        // display the map in string
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                switch (map[i][j]) {
                case meet:
                    sb.append("1"); // if player i and j meets, put 1 in the map //this line causes error
                default:
                    sb.append("0"); // if they haven't met, put the 0 as default
                }
                if (j < n - 1)
                    sb.append("|");
            }
            if (i < n - 1)
                sb.append("\n-----\n");
        }
        return sb.toString();
    }
}

class MultiplayerTest {
    public static void main(String[] args) {
        Multiplayer newGame = new Multiplayer();
        newGame.n = 5; // test for a small number of players
        // test for player 1 to meet all other players
        for (int i = 2; i <= 5; i++) {
            newGame.meet(1, i);
        }
        // print test to see if player 1 wins the game
        System.out.println(newGame.toString());
        System.out.println(newGame.isWin());

    }
}

最佳答案

不能使用变量作为 switch 语句中的标签。

如果你想在程序中拥有一个常量值,通常的方法是创建一个静态final成员:

public static final int MEET = 1;

但是,在这种情况下,如果只有两个值,那么最好使用 boolean 数组并仅检查 true 和 false。不需要 switch 语句。

boolean map[][] = new boolean[n][n];

...

if (map[i][j]) {
    ...

在这种情况下,最好重命名数组 met,因此代码如下所示:

if (met[i][j]) {
    ...

关于java - (二维数组)Java错误: constant expression required,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54627190/

相关文章:

c# - 如何使用重复键对列表进行排序?

data-structures - 如何在特征的函数中发送不同的结构?

java - 当我已经声明了该类时,为什么我需要 = (class) ...?

Golang LinkedList 删除第一个元素

c++ - std::map<t1, t2>::erase(iterator position) 的工作?

java - MapFragment - Google map 内存不足错误

c - C 中的内存压缩器?

java - 哪些三字母时区 ID 没有被弃用?

java - JavaMelody 和 ab 中的 tomcat 性能监控

java - java中二维数组出错