不兼容的类型:“int []变量无法转换为ArrayList <string>”

时间:2016-07-07 09:20:00

标签: java arraylist

目标:将所有计算机中的所有计算机沉入5个猜测中,给出评分你的表现如何,即numOfGuesses

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

它是如何工作的:这一切都适用于cmd-line,计算机将要求你输入一个猜测(一个单元格),你将在cmd-line键入“0”,“2”等。根据你的猜测,你会在cmd-line上看到一个结果,无论是“命中”,“小姐”还是“杀死”,当你杀死游戏结束打印你的数字猜测时杀死的值即变量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");

}
}
}
}

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

4 个答案:

答案 0 :(得分:0)

您需要将int[]转换为ArrayList<String>因为您的方法参数要求ArrayList<String>,而不是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;
}

使用int[]作为{ 1, 2, 3 }的输入进行测试,List<String>的输出为[1, 2, 3]

答案 1 :(得分:0)

正如在评论中已经说明的那样,你试图为一个函数提供一个int数组,该函数将String的StringList作为参数。

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

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

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

答案 2 :(得分:0)

您的setLocationCells函数接受ArrayList<String>,但您给它int[]。您的评论说该函数接受int[],因此您应该将参数更改为int[]。您还需要locationCellsint[]

答案 3 :(得分:-1)

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