【问题标题】:Head First Java book battleship gameHead First Java书战舰游戏
【发布时间】:2019-08-26 15:14:03
【问题描述】:

我正在阅读“Head First Java”一书,我在第 5 章的战舰游戏(简单版)中遇到了问题。我知道这本书的代码不起作用,我尝试自己修复它,但它仍然不起作用。

所以尝试谷歌它,我在这个网站上找到了一些帖子,但我仍然有问题。游戏无法正常运行。

如果玩家输入任何随机数,输出总是“命中”...

这是最后一个版本的代码:

DotCom 类:

public class DotCom {

    private ArrayList<String> locationCells = new ArrayList<>();

    public void setlocationCells(int[] loc) {
        if (loc != null)
            for (int val : loc)
                locationCells.add(String.valueOf(val));
    }

    public String checkYourself(String userInput) {

        String result = "miss";
        int index = locationCells.indexOf(userInput);

        if (index >= 0) {
            locationCells.remove(index);
        }

        if (locationCells.isEmpty()) {
            result = "kill";
        } else {
            result = "hit";
        }
        System.out.println(result);
        return result;
    }
}

DotComGame 类:

public class DotComGame {

    public static void main(String[] args) {
        int guessingTimes = 0;
        DotCom dot = new DotCom();
        GameHelperrr helper = new GameHelperrr();

        int randomNum = (int) (Math.random() * 5);

        int[] locations = { randomNum, randomNum + 1, randomNum + 2 };

        dot.setlocationCells(locations);

        boolean isAlive = true;

        while (isAlive == true) {

            String guess = helper.getUserInput("Enter a number");
            String result = dot.checkYourself(guess);
            guessingTimes++;

            if (result.equals("kill")) {
                isAlive = false;
                System.out.println("You took " + guessingTimes + " guesses");

            }
        }
    }
}

我真的很感激能得到一个详细且易于理解的答案,因为我被困住了,几天都无法继续阅读这本书。

【问题讨论】:

  • 什么版本的“Head First Java”?那是一本比较老的书。如果它没有假设 JDK 8 或更高版本,请小心。

标签: java arraylist


【解决方案1】:

int index = locationCells.indexOf(userInput);

如果集合中不存在该元素,此方法将返回 -1

所以如果你错过了,它不会达到这个条件:

if (index >= 0) {
   locationCells.remove(index);
}

此集合中仍有元素,因为您没有删除任何内容...

if (locationCells.isEmpty()) {
    result = "kill";
} else {
    result = "hit";
}

所以在未命中时,结果仍然显示“命中”。

试试这个:

if (locationCells.isEmpty()) {
   result = "kill";
} else {
   result = index == -1 ? "miss" : "hit";
}

如果你没有击杀对手的舰船,那么你要么错过所有舰船,要么击中一艘舰船。

【讨论】:

  • 非常感谢,但是你能解释一下“result = index == -1”的语法吗?“miss”:“hit”;“它是如何工作的,它是做什么的,因为我不明白
  • 本质上是说如果索引为-1,那么结果应该是“未命中”。否则就是“命中”。研究ternary operatorbaeldung.com/java-ternary-operator
【解决方案2】:

我猜 checkYourself-Method 应该是这样的:

public String checkYourself(String userInput) {

    String result = "miss";

    int index = locationCells.indexOf(userInput);

    if(index >= 0) {
        locationCells.remove(index);
        if (locationCells.isEmpty()) {
            result = "kill";
        }else {
            result = "hit";
        }
    }

    System.out.println(result);

    return result;
}

在当前形式中,ArrayList 永远不会为空,因为您插入 3 个值,但如果用户输入在列表中,则仅删除 1,因此 .isEmpty() 永远不会为 TRUE。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多