我是新手,跟随着这本书的第一篇Java',并按照本书中的一个项目。我在main方法的最后一行遇到错误 - "方法checkYourself(String)未定义类型String"
class dotcom{
int [] Locationcells;
int hits = 0;
public String checkYourself(String guess){
int guess1 = Integer.parseInt(guess);
String result = "miss";
for(int cell : Locationcells){
if(guess1 == cell){
result = "hit";
hits = hits + 1;
break;
}
}
if (hits == Locationcells.length){
result = "kill";
}
System.out.println(result);
return result;
}
public void setCellLocations(int []locs){
Locationcells = locs;
}
}
public class SimpleDotComGame {
public static void main(String[] args) {
dotcom dotMan = new dotcom();
int locations [] = {3,4,5};
dotMan.setCellLocations(locations);
String userguess = "2";
String result = userguess.checkYourself(userguess);
}
}
答案 0 :(得分:1)
您正在为字符串值
调用方法checkYourself
userguess.checkYourself(userguess);
... String类不存在。
应该是
dotMan.checkYourself(userguess);
答案 1 :(得分:0)
你想做
String result = dotman.checkYourself(userguess);
而不是
String result = userguess.checkYourself(userguess);
错误消息表示checkYourself
不是String
类中的方法。
答案 2 :(得分:0)
result = dotMan.checkYourself(userguess)