public class SimpleDotComTestDrive {
public static void main(String[] args) {
// 创建对象
SimpleDotCom dot = new SimpleDotCom ();
//指定位置
int[] locations = {2,3,4};
dot.setLocationsCells(locations);
//伪造错误猜测
String userGuess = "2";
//检查猜测值,传值
String result = dot.checkYourself(userGuess);
}
}
public class SimpleDotCom {
int[] locationCells;
int numOfHits = 0;
public void setLocationsCells(int[] locs) {
locationCells = locs;
}
public String checkYourself(String stringGuess)
{
//将猜测结果转换为int
int guess = Integer.parseInt(stringGuess);
String result = "miss";
//locationCells元素都判断一次,并赋给cell
for(int cell : locationCells) {
if(guess == cell) {
result = "hit"; //命中!!
numOfHits++;
break; //中止循环,判断是否took down
}
}
if(numOfHits == locationCells.length) {
result = "kill"; //whether took down
}
System.out.println(result); //show result
return result;
}
}