0
public static String[] getWords(int cat, int diff) {
    String topic[][][] = new String[3][3][3];
    switch(cat){
        case 0:
            topic[0][0][0] = "Paris";
            topic[0][0][1] = "London";
            topic[0][0][2] = "Sydney";
            diff = 0;
            topic[0][1][0] = "Toronto";
            topic[0][1][1] = "Florida";
            topic[0][1][2] = "Frankfurt";
            diff = 1;
            topic[0][2][0] = "Barcelona";
            topic[0][2][1] = "Vancouver";
            topic[0][2][2] = "Zimbabwe";
            diff = 2;
        case 1:
            topic[1][0][0] = "Halo";  
            topic[1][0][1] = "Fifa";                   
            topic[1][0][2] = "GTA";
            diff = 0;                  
            topic[1][1][0] = "Skyrim";
            topic[1][1][1] = "HITMAN";
            topic[1][1][2] = "Batman";
            diff =1;
            topic[1][2][0] = "Minecraft";
            topic[1][2][1] = "Zombieville";
            topic[1][2][2] = "BoderLands";
            diff =2;
        case 2:
            topic[2][0][0] = "Acura";
            topic[2][0][1] = "Audi";
            topic[2][0][2] = "Bmw";
            diff = 0;
            topic[2][1][0] = "Bentley";
            topic[2][1][1] = "Buggati";
            topic[2][1][2] = "Honda";
            diff = 1;
            topic[2][2][0] = "Lamborghini";
            topic[2][2][1] = "Rolls-Royce";
            topic[2][2][2] = "Mercedes";
            diff = 2;
    }
    return topic[cat][diff];
}

所以这是我使用 3d 数组的方法,我想知道我是否做对了,如果我在 main 方法中调用它会起作用吗?我使用了 switch 语句,因为有人向我推荐了它,我真的是 java 新手,你可以看到这是游戏 Hangman

4

2 回答 2

3

不,不会的。您的 switch 不包含任何break;语句,因此在它遇到第一个case评估为 true 的语句后,它将执行之后的所有语句,直到 switch 结束,或者直到你break;return其他东西。

此外,调用:

diff = 0;
// ...
diff = 1;
// ...
diff = 2;

是多余的,因为分配的值从未使用过。

所以应该是:

switch(cat)
{
    case 0:
        topic[0][0][0] = "Paris";
        topic[0][0][1] = "London";
        topic[0][0][2] = "Sydney";

        topic[0][1][0] = "Toronto";
        topic[0][1][1] = "Florida";
        topic[0][1][2] = "Frankfurt";

        topic[0][2][0] = "Barcelona";
        topic[0][2][1] = "Vancouver";
        topic[0][2][2] = "Zimbabwe";
        break;
    case 1:
        topic[1][0][0] = "Halo";
        topic[1][0][1] = "Fifa";
        topic[1][0][2] = "GTA";

        topic[1][1][0] = "Skyrim";
        topic[1][1][1] = "HITMAN";
        topic[1][1][2] = "Batman";

        topic[1][2][0] = "Minecraft";
        topic[1][2][1] = "Zombieville";
        topic[1][2][2] = "BoderLands";
        break;
    case 2:
        topic[2][0][0] = "Acura";
        topic[2][0][1] = "Audi";
        topic[2][0][2] = "Bmw";

        topic[2][1][0] = "Bentley";
        topic[2][1][1] = "Buggati";
        topic[2][1][2] = "Honda";

        topic[2][2][0] = "Lamborghini";
        topic[2][2][1] = "Rolls-Royce";
        topic[2][2][2] = "Mercedes";
        break;
}
于 2013-01-10T22:20:54.907 回答
1

你需要休息一下;在情况之间。它只会简单地处理每个案例,因为没有休息时间。好吧,如果是案例 1,则每个案例,如果是案例 2,则为 2 和 3,并且只有案例 3。

所以 3 是唯一可以正常中断的,因为因为它是最后一个,所以你不需要一个。!

于 2013-01-10T22:28:46.317 回答