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条)

报告相同问题?

悬赏问题

  • ¥15 chaquopy python 安卓
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题