Java 不兼容类型:“;int[]变量无法转换为ArrayList<;字符串>&引用;

Java 不兼容类型:“;int[]变量无法转换为ArrayList<;字符串>&引用;,java,arraylist,Java,Arraylist,目标:在5次猜测中排除所有计算机网络通信,你将获得你的ie猜测表现的评分 import java.util.Scanner; import java.util.ArrayList; class DotCom { private ArrayList<String> locationCells; // to hold the location cells public void setLocationCells(ArrayList<String

目标:在5次猜测中排除所有计算机网络通信,你将获得你的ie猜测表现的评分

    import java.util.Scanner;

import java.util.ArrayList;

class DotCom
{

    private ArrayList<String> locationCells;    // to hold the location cells

    public void setLocationCells(ArrayList<String> loc) //setter method that takes an int array(which has three cell locations as ints(2,3,4,etc))
{

        locationCells = loc;

}

    public String checkYourself(String userInput)   //method that takes a String for the user's Input("1","3",etc).checks it and returns a result representing a "hit","miss" or "kill".
{

        String result = "miss"; //when you miss hit the randomNum value generated

        int index = locationCells.indexOf(userInput);   //checks the index of the userInput(user's Input),from the locationCells and Stores the value in index int variable

        if(index >= 0)  
{

            locationCells.remove(index);    //removes the index position(user's guess)from the array,so that the same value don't get accepted again 

        if(locationCells.isEmpty()) //if locationCells array goes empty
{

            result = "kill"; // when you hit all the three randomNum values

}
        else
{

            result = "hit"; //when you  hit the randomNum value

}
}
            System.out.println(result); //print result
            return result;

}
}
class DotComTestDrive
{

    public static void main(String []args)
{

        Scanner user_input = new Scanner(System.in);

            int numOfGuesses = 0;   //for storing user guesses

            DotCom dot = new DotCom();  //dot com instance variable

            int randomNum = (int)(Math.random()*5); //to get a random value as an int variable and store in randomNum variable

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

            dot.setLocationCells(location);

            boolean isAlive = true;

    while(isAlive == true  && numOfGuesses < 6) 
{

            System.out.println("Enter Your Guess : ");

            String userInput = user_input.next();   //take user input(user's guess)

            String result = dot.checkYourself(userInput);

            numOfGuesses++;

    if(result.equals("kill"))
{

            isAlive = false;

            System.out.println("You Took " + numOfGuesses + " guesses");

}
}
}
}
设置:当程序启动时,计算机会在虚拟7X1网格上放置三个点com。完成后,游戏会询问您的第一个猜测

工作原理:整个过程在命令行上运行,计算机会要求您输入一个猜测(一个单元格),您将在命令行上键入“0”、“2”等。根据您的猜测,您将在命令行上看到一个结果,“命中”、“未命中”或“杀死”,当你被杀死时,游戏结束打印你杀死的猜测数,即变量numOfGuesses的值

    import java.util.Scanner;

import java.util.ArrayList;

class DotCom
{

    private ArrayList<String> locationCells;    // to hold the location cells

    public void setLocationCells(ArrayList<String> loc) //setter method that takes an int array(which has three cell locations as ints(2,3,4,etc))
{

        locationCells = loc;

}

    public String checkYourself(String userInput)   //method that takes a String for the user's Input("1","3",etc).checks it and returns a result representing a "hit","miss" or "kill".
{

        String result = "miss"; //when you miss hit the randomNum value generated

        int index = locationCells.indexOf(userInput);   //checks the index of the userInput(user's Input),from the locationCells and Stores the value in index int variable

        if(index >= 0)  
{

            locationCells.remove(index);    //removes the index position(user's guess)from the array,so that the same value don't get accepted again 

        if(locationCells.isEmpty()) //if locationCells array goes empty
{

            result = "kill"; // when you hit all the three randomNum values

}
        else
{

            result = "hit"; //when you  hit the randomNum value

}
}
            System.out.println(result); //print result
            return result;

}
}
class DotComTestDrive
{

    public static void main(String []args)
{

        Scanner user_input = new Scanner(System.in);

            int numOfGuesses = 0;   //for storing user guesses

            DotCom dot = new DotCom();  //dot com instance variable

            int randomNum = (int)(Math.random()*5); //to get a random value as an int variable and store in randomNum variable

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

            dot.setLocationCells(location);

            boolean isAlive = true;

    while(isAlive == true  && numOfGuesses < 6) 
{

            System.out.println("Enter Your Guess : ");

            String userInput = user_input.next();   //take user input(user's guess)

            String result = dot.checkYourself(userInput);

            numOfGuesses++;

    if(result.equals("kill"))
{

            isAlive = false;

            System.out.println("You Took " + numOfGuesses + " guesses");

}
}
}
}
import java.util.Scanner;
导入java.util.ArrayList;
类网络公司
{
private ArrayList locationCells;//用于保存位置单元格
public void setLocationCells(ArrayList loc)//采用int数组的setter方法(该数组有三个单元格位置作为int(2,3,4等))
{
locationCells=loc;
}
publicstringcheckyourself(stringuserinput)//将字符串作为用户输入(“1”、“3”等)的方法。检查该字符串并返回表示“命中”、“未命中”或“杀死”的结果。
{
String result=“miss”;//未命中时,单击生成的randomNum值
int index=locationCells.indexOf(userInput);//从locationCells检查userInput(用户输入)的索引,并将值存储在index int变量中
如果(索引>=0)
{
locationCells.remove(index);//从数组中删除索引位置(用户的猜测),以便不再接受相同的值
if(locationCells.isEmpty())//如果locationCells数组为空
{
result=“kill”//当您点击所有三个randomNum值时
}
其他的
{
result=“hit”//当您点击randomNum值时
}
}
System.out.println(结果);//打印结果
返回结果;
}
}
类DotComTestDrive
{
公共静态void main(字符串[]args)
{
扫描仪用户输入=新扫描仪(System.in);
int numOfGuesses=0;//用于存储用户猜测
DotCom dot=new DotCom();//DotCom实例变量
int randomNum=(int)(Math.random()*5);//获取随机值作为int变量并存储在randomNum变量中
int[]location={randomNum,randomNum+1,randomNum+2}//
dot.setLocationCells(位置);
布尔值=真;
while(isAlive==true&&numfoguesses<6)
{
System.out.println(“输入您的猜测:”);
字符串userInput=user_input.next();//获取用户输入(用户猜测)
字符串结果=dot.checkYourself(userInput);
numOfGuesses++;
if(result.equals(“kill”))
{
isAlive=假;
System.out.println(“您接受了”+numOfGuesses+“guesses”);
}
}
}
}

“int[]变量无法转换为ArrayList”获取以下行“dot.setLocationCells(location);”的上述错误“

您的setLocationCells(ArrayList loc)将ArrayList作为参数,您正试图在其中发送int[]。因此出现错误

您需要将
int[]
转换为
ArrayList
,因为您的方法参数要求的是
ArrayList
,而不是
int[]
。为此,请执行以下操作:

private List<String> convertIntArr(int[] input) {
    List<String> output = new ArrayList<>(input.length);
    for (int x : input) {
        output.add(String.valueOf(x));
    }
    return output;
}
private List convertIntArr(int[]输入){
列表输出=新的ArrayList(input.length);
for(int x:输入){
add(String.valueOf(x));
}
返回输出;
}

测试时输入
int[]
作为
{1,2,3}
,输出
List
作为
[1,2,3]

,如注释中所述,您试图为以字符串数组列表作为参数的函数提供一个int数组

我不知道哪一种是正确的类型,但无论哪种方式,您都需要提供正确的类型。如果函数确实应该采用
ArrayList
(从代码的其余部分来看,这似乎是可能的),那么应该替换该行

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

ArrayList location=new ArrayList();
add(String.valueOf(randomNum));
add(String.valueOf(randomNum+1));
add(String.valueOf(randomNum+2));

您的setLocationCells函数接受一个
ArrayList
,但您给它一个
int[]
。您的注释表示该函数接受
int[]
,因此您可能应该将参数更改为
int[]
。您还需要将
locationCells
设置为
int[]

是的,错误信息似乎相当清楚-您预期会发生什么?你知道
int[]
ArrayList
是完全不同的类型吗?疯狂缩进是怎么回事?@khelwood我见过你多次询问“疯狂意图”。虽然我必须承认我觉得这有点可笑,但我不认为这真的算得上是“关注帖子,而不是人。这包括那些即使在帖子上使用也会让人觉得很私人的术语(比如“懒惰”、“无知”或“发牢骚”)。“疯狂”感觉就像是属于我的。也许只是考虑放弃它吧?我们对你的游戏并不感兴趣。如果您有与编程相关的问题,请提问,并提供相关代码(而不是完整的“游戏”)。请提出解决方案。你只是在重复问题中的错误。