(2D数组)Java错误:需要常量表达式

(2D数组)Java错误:需要常量表达式,java,data-structures,Java,Data Structures,我正在做数据结构的家庭作业,但我在2D数组方面遇到了问题。我要做的是: “假设您正在设计一款具有n≥1000名球员, 编号为1到n,在魔法森林中互动。本游戏的获胜者 游戏是第一个至少能和所有其他玩家见面的玩家 一次(允许连接)。假设有一个方法满足(i,j), 每当一个玩家i遇到一个玩家j(i̸=j)时,它就被称为, 请描述一种方法,以跟踪两对会面玩家以及谁是赢家。” 由于以下错误,我无法编译: java:51:错误:需要常量表达式 对于这一行: case meet: sb.append("1")

我正在做数据结构的家庭作业,但我在2D数组方面遇到了问题。我要做的是:

“假设您正在设计一款具有n≥1000名球员, 编号为1到n,在魔法森林中互动。本游戏的获胜者 游戏是第一个至少能和所有其他玩家见面的玩家 一次(允许连接)。假设有一个方法满足(i,j), 每当一个玩家i遇到一个玩家j(i̸=j)时,它就被称为, 请描述一种方法,以跟踪两对会面玩家以及谁是赢家。”

由于以下错误,我无法编译:

java:51:错误:需要常量表达式

对于这一行:

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

    }
}
/*假设您正在设计一款具有n≥1000
玩家,编号为1到n,在魔法森林中互动。胜利者
这个游戏的玩家是第一个可以在同一时间遇到所有其他玩家的玩家
至少一次(允许系领带)。假设有一个方法满足(i,
j) ,每当一个玩家i遇到一个玩家j(与i̸=
j) ,描述一种跟踪成对会面玩家的方法,以及
谁是赢家*/
公共类多人游戏{
int n;//玩家数量
int-map[][]=新建int[n][n];//创建二维数组
int满足=1;
int notMeet=0;
int[]count;//保持计数的数组,玩家在到达n时获胜
公共多人游戏(){
clearMap();
}//启动一个新游戏
public void clearMap(){//清除二维数组
对于(int i=0;i对于(inti=2;i,不能在switch语句中使用变量作为标签

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

...

if (map[i][j]) {
    ...
如果希望在程序中具有常量值,通常的方法是创建静态最终成员:

public static final int MEET = 1;
但是,在这种情况下,如果只有两个值,那么最好使用布尔数组,只检查true和false,而不需要switch语句

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

...

if (map[i][j]) {
    ...
在这种情况下,最好重命名数组met,因此代码如下所示:

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

不能将变量用作switch语句中的标签

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

...

if (map[i][j]) {
    ...
如果希望在程序中具有常量值,通常的方法是创建静态最终成员:

public static final int MEET = 1;
但是,在这种情况下,如果只有两个值,那么最好使用布尔数组,只检查true和false,而不需要switch语句

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

...

if (map[i][j]) {
    ...
在这种情况下,最好重命名数组met,因此代码如下所示:

if (met[i][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
}
请注意,没有
break;
语句,因此,根据
fall-through
逻辑,它将附加
0

如果您只有一个
案例
,则应使用
If
,例如:

if(map[i][j] == meet) {
    sb.append("1");
}else {
    sb.append("0");
}
更新

关于
NullPointerException
,您将获得该异常,因为
count
数组未初始化,并且您正在尝试访问该元素

将声明更改为以下内容:

int[] count = new int[n];
在下面的代码片段中:

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
}
请注意,没有
break;
语句,因此,根据
fall-through
逻辑,它将附加
0

如果您只有一个
案例
,则应使用
If
,例如:

if(map[i][j] == meet) {
    sb.append("1");
}else {
    sb.append("0");
}
更新

关于
NullPointerException
,您将获得该异常,因为
count
数组未初始化,并且您正在尝试访问该元素

将声明更改为以下内容:

int[] count = new int[n];

您需要初始化
计数
数组,如

public void initializeArray(int n) {
    this.n = n;
    count = new int[n];
    for (int i = 0; i < n; i++) count[i] = 0;
}
您需要执行以下操作:

initializeArray(5);

您需要初始化
计数
数组,如

public void initializeArray(int n) {
    this.n = n;
    count = new int[n];
    for (int i = 0; i < n; i++) count[i] = 0;
}
您需要执行以下操作:

initializeArray(5);

我看不出您实际在哪里创建数组计数。 您的代码: 布尔集合[][]=新布尔值[n][n];//已编辑 int[]count;//保持计数的数组,玩家在到达n时获胜
创建数组met(…=new boolean[n][n]),但是数组计数没有这样的语句。因此,对count[i]的引用失败。

我看不出您实际在哪里创建数组计数。 您的代码: 布尔集合[][]=新布尔值[n][n];//已编辑 int[]count;//一个数组到k