如何在游戏Hangman中使用3D阵列?

时间:2013-01-10 20:46:04

标签: java arrays multidimensional-array

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数组并有3个选项,其中包括级别diffivculty和类别,我如何调用它并使用它,好像该人选择此...它将通过数组并选择我将把它放在一个方法中并在main方法中调用它

1 个答案:

答案 0 :(得分:2)

所以你问如何使用你的阵列?最简单的方法是创建一个方法,返回存储在适当类别和难度的值

public String[] getWords(int cat, int diff)
{
 return topic[cat][diff];
 }

您可以调用该方法,然后从返回的数组中选择一个供玩家猜测的单词。

更新:

我准备好了,并说你在编程方面很有新意,而且你还不明白如何使用方法。 Some light reading might help with that.您可以在main方法的外侧定义方法,并调用以检索给定玩家的单词。传入两个整数,然后用于指定要访问的数组中的位置。