Xiongzhizhu 2017-11-10 01:47 采纳率: 100%
浏览 1416
已采纳

ArrayList参数类型的匹配问题

import java.util.ArrayList;
public class SimpleDotCom {
private ArrayList locationCells;
public void setLocationCells(ArrayList loc) {
locationCells = loc;
}

public String checkYourself(String userinput)  {
    int index = locationCells.indexOf(userinput);
    String result = "miss";
    if(index >= 0){
        locationCells.remove(index);
        if(locationCells.isEmpty()) {
            result = "kill";
        }else{
            result = "hit";
        }
    }
    System.out.println(result);
    return result;
}

}

public class SimpleDotComGame {

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

    SimpleDotCom theDotCom = new SimpleDotCom();
    int randomNum = (int) (Math.random() * 5);

    int[] locations = {randomNum, randomNum + 1, randomNum + 2};
    theDotCom.setLocationCells(locations);
    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");
        }
    }
}

}

报错显示为:Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method setLocationCells(ArrayList) in the type SimpleDotCom is not applicable for the arguments (int[])

at SimpleDotComGame.main(SimpleDotComGame.java:12)
求大佬解答
  • 写回答

6条回答

  • fuweihua123 2017-11-10 08:02
    关注

    兄弟!我看了看,你的意思是随机生成3个连续的数可能是【0,1,2】【1,2,3】.....【4,5,6】等等。然后你自己手动输入一个数,判断这个输入的数是否在随机生成的3个数中,如果是的话,把输入的数从集合中删除,并打印“hit”,否则打印“miss”.
    我亲手测了一下没问题了。
    首先1,你不能i传个nt[]数组类型进去,其次还有其他问题,你创建集合必须用泛型《string》,这样你添加进去的元素是string,才能调用indexof方法
    代码如下:图片说明

    如果有问题找我。。我是为积分来的
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码