不兼容的类型:“INT []变量不能转换为ArrayList的

问题描述:

目标:水槽所有的电脑点COMS在5个猜测,你是给你的表现如何,即numOfGuesses不兼容的类型:“INT []变量不能转换为ArrayList的<String>”

评级

设置:当程序电脑将虚拟7X1网格放置三个dotcom。当完成时,游戏要求您进行第一次猜测。

工作原理:整件事情在cmd-line上工作,计算机会要求你输入一个猜测(单元格),你会在cmd行输入“0”,“2”等等。为了回应你的猜测,你会在cmd行看到一个结果,或者是“Hit”,“Miss”或者“kill”,当你杀死游戏结束时,打印你的数量,可变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(位置);”

+1

好吧,那么错误信息似乎很合理清楚 - 你期望发生什么?你知道'int []'和'ArrayList '是完全不同的类型吗? –

+1

http://*.com/help/how-to-ask? –

+1

什么是疯狂的缩进? – khelwood

你setLocationCells函数接受一个ArrayList<String>,但你给它一个int[]。你的评论说该功能接受int[],所以你应该改变参数为int[]。您还需要制作locationCellsint[]

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

+4

请提出解决方案。你只是重申问题中的错误。 –

你需要,因为你的方法参数要求为ArrayList<String>,不是int[]转换您int[]ArrayList<String>。这样做的:

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]

正如评论中所述,您试图为以String的ArrayList作为参数的函数提供一个int数组。

我不知道哪一个是正确的类型,但是您需要提供正确的类型。如果函数确实应该采取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));