gpt4 book ai didi

java - Head First Java p.139 战舰 ArrayList<> 问题

转载 作者:太空宇宙 更新时间:2023-11-04 10:24:41 25 4
gpt4 key购买 nike

我是一个想学习Java的菜鸟。我正在阅读《Head First Java》一书,并且非常喜欢它。我在学习使用 ArrayList<> 时遇到了一个问题而不是常规的array[] 。我在尝试分配 int[] 时遇到异常到ArrayList<> ,请看下面的代码:

import java.util.ArrayList;

public class SimpleDotComGame {

public static void main(String[] args) {
int numOfGuesses = 0;
GameHelper helper = new GameHelper();

DotCom TheDotCom = new DotCom();
int randomNum = (int) (Math.random()*5);
int[] locations = {randomNum, randomNum+1, randomNum+2};
TheDotCom.setLocationCells(locations); <---- Here is the problem.
boolean isAlive = true;

while(isAlive == true) {
String guess = helper.getUserInput("enter a number");
String result = TheDotCom.checkYourself(guess);
numOfGuesses++;
if (result.equals("Kill")) {
isAlive = false;
System.out.println("You took " + numOfGuesses + " guesses, to"
+ "destroy the DotCom");

}
}
}
}

异常内容如下:

  Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
The method setLocationCells(ArrayList<String>) in the type DotCom is not applicable for the arguments (int[])

我尝试更改 int[] locations = {}进入ArrayList<String>查找此问题后,发现只有一个人询问过,使用以下代码:

ArrayList<String> locations = new ArrayList<String>();
String r1 = Integer.toString(randomNum);
String r2 = Integer.toString(randomNum+1);
String r3 = Integer.toString(randomNum+2);
locations.add(r1);
locations.add(r2);
locations.add(r3); ArrayList<String> locations = new ArrayList<String>;

这解决了异常,程序将运行,但在命令行中输入猜测不会返回“命中、未命中或杀死”值,并且我可以猜测任何数字。换句话说,战舰这个游戏不行。

供您引用,DotCom 类是:

import java.util.ArrayList;

public class DotCom {

private ArrayList<String> locationCells;
// private int numOfHits;
// don't need that now.

public void setLocationCells(ArrayList<String> loc) {
locationCells = loc;
}

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";
}
}

return result;
}

}

您能提供的任何建议都会很棒。我很紧张,在第 5 章中,我真的很难自己想出可用的代码行,但我希望这对于几乎没有经验的人来说是正常的?我还担心这个问题没有其他人问过!

最佳答案

看来你必须重写现有的方法 setLocationCells新的,接受 int[] loc 。另外,最好隐藏locationCells里面DotCom将其修改排除在此类之外。

class DotCom {

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

public void setLocationCells(List<String> loc) {
locationCells.clear();

if (loc != null)
locationCells.addAll(loc);
}

public void setLocationCells(int[] loc) {
locationCells.clear();

if(loc != null)
for(int val : loc)
locationCells.add(String.valueOf(val));
}

}

如果您无法修改DotCom ,那么你可以转换 int[]进入ArrayList<String>在客户端使用 Stream :

TheDotCom.setLocationCells((ArrayList<String>)Arrays.stream(locations).boxed().map(String::valueOf).collect(Collectors.toList()));

附注在您的示例中,您甚至不必转换 int[]进入ArrayList<String> ,因为您可以创建 ArrayList<String>而不是int[] ,尤其是当您只有 3 时该数组中的值。

public static void main(String... args) throws IOException {
GameHelper helper = new GameHelper();
DotCom dotCom = new DotCom();

// create and add locations using random generator
int rnd = (int)(Math.random() * 5);
dotCom.setLocationCells(new ArrayList<>(Arrays.asList(String.valueOf(rnd), String.valueOf(rnd + 1), String.valueOf(rnd + 2))));

boolean alive = true;
int numOfGuesses = 0;

while (alive) {
String result = dotCom.checkYourself(helper.getUserInput("enter a number"));
numOfGuesses++;

if ("Kill".equals(result)) {
alive = false;
System.out.println("You took " + numOfGuesses + " guesses, to" + "destroy the DotCom");
}
}
}

关于java - Head First Java p.139 战舰 ArrayList<> 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50679726/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com