JAVA:文字游戏查询

时间:2014-01-29 00:35:07

标签: java arrays integer game-engine

你好我的同事们!

到目前为止我有这个代码:

public class Levels {
int cLevel;

public void newGame() {
    boolean newGame = true;
    if (newGame) {
        cLevel = 1;

        List<String> list = new ArrayList<String>();

        try {
            BufferedReader bf = new BufferedReader(new FileReader(
                    "src/WordGuess/ReadFile/LevelFiles/Level_" + cLevel
                            + ".txt"));
            String cLine;
            while ((cLine = bf.readLine()) != null) {
                list.add(cLine);
            }
            String words[] = new String[list.size()];
            words = list.toArray(words);

            System.out
                    .println("These are your help letters so you can beat that level: "
                            + words[0]);

            for (int i = 1; i < words.length; i++) {
                char[] replaceChar = words[i].toCharArray();

                for (int x = 0; x < replaceChar.length; x++) {
                    replaceChar[x] = '*';
                }
                String replacement = new String(replaceChar);

                System.out.println("\f" + replacement);

            }

            System.out.println("\t Score: " + Score(0) + ";\t Lives: "
                    + Lives(5) + ";");

            System.out.println("\n Enter Word:");
            System.in.read();

        } catch (Exception e) {
            System.out
                    .println("Oh! Something went terribly wrong. A team of highly trained and koala-fied koalas have been dispatched to fix the problem. If you dont hear from them please restart this program.");
            e.printStackTrace();
        }
    }
}

private int Lives(int lives) {
    int wrongTries = 0;
    boolean correctWord;
    int counter = 0;

    String livesForm[] = new String[lives];

    // for (int i = 0; i == lives; i++) {
    // livesForm[i] = "♥";
    // }

    return lives;
}

private int Score(int score) {

    return score;
}}

所以我的查询如下。

我想将生命显示为心脏符号,并且仍然保留其整数值以用于功能目的。因此,如我的代码示例所示,5生命由5个心脏代表,但仍然保持3生命值。如我的例子中所示,我也尝试了一个字符串数组,其大小为整数值'生命'并且被心所取代。至于我运行代码没有显示任何错误,所以我认为数组已经填充但我无法弄清楚如何在控制台中显示数组为“生活:♥♥♥♥♥”。

我试过常规:

System.out.println(livesForm[i]);

但它没有显示任何东西。我不知道我是否完全清楚地构建了我的问题。如果你们中的任何一个人能给我一个建议或提示,我将很高兴。感谢您花时间在这个问题上!

1 个答案:

答案 0 :(得分:1)

为什么不:

public String getLivesAsString(int lives) {
    StringBuilder sb = new StringBuilder(lives);
    for(int i = 0; i < lives; i++) {
        sb.append("♥");
    }
    return sb.toString();
}

不确定为什么你需要一个数组来显示由整数定义的'♥' - 符号量?