Goal "int [] Variable kann nicht auf Arraylist
Setup durchführen: wenn Programm gestartet, platziert der Computer drei Dotcoms auf dem virtuellen 7X1-Gitter. Wenn das abgeschlossen ist, fragt das Spiel nach Ihrer ersten Schätzung.
Wie es funktioniert: Diese ganze Sache funktioniert auf cmd-Linie, der Computer wird Sie bitten, eine Vermutung (eine Zelle) einzugeben, dass Sie in der cmd-Zeile als "0", "2" eingeben usw. Als Antwort auf Ihre Vermutung werden Sie ein Ergebnis in der cmd-Zeile sehen, entweder "Hit", "Miss" oder "kill". Wenn Sie kill erhalten, wird die Anzahl der angenommenen Werte zum Töten angezeigt der variable 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");
}
}
}
}
den obigen Fehler für die folgende Zeile "int [] Variable nicht auf Arraylist umgewandelt werden kann" getting "dot.setLocationCells (Ort);"
Richtig, nun scheint die Fehlermeldung einigermaßen klar zu sein - was haben Sie erwartet? Verstehst du, dass 'int []' und 'ArrayList' völlig unterschiedliche Typen sind? –
http://stackoverflow.com/help/how-to-ask? –
Was ist mit der verrückten Einrückung? – khelwood