public class game {
int[] locks;
int numberofhits = 0;
boolean isAlive = true;
int numberofguess = 0;
public void setlocation(int[] location) {
locks = location;
}
public String checkyourself(String guess) {
int guess1 = Integer.parseInt(guess);
String result = "missed";
System.out.println("Guess by Player =" + guess);
for (int cell : locks) {
if (guess1 == cell) {
result = "killed";
isAlive = false;
numberofhits++;
break;
}//if close
}//for close
System.out.println("Player you have " + result + " the object");
while (isAlive == true) {
System.out.println("Enter another guess buddy");
break;
}
while (isAlive == false) {
System.out.println("You hit the object");
break;
}
System.out.println("Number of object hit by the player is " + numberofhits);
return result;
}//method close
}//class close
public class rungame {
public static void main(String[] args) {
int randomnumber = (int) (Math.random() * 8);
int[] location = {randomnumber, randomnumber + 1,
randomnumber + 2, randomnumber + 3};
game player = new game();
player.setlocation(location);
player.checkyourself("0");
System.out.println("Game is Started ...........");
player.checkyourself("1");
player.checkyourself("2");
player.checkyourself("3");
player.checkyourself("4");
player.checkyourself("5");
player.checkyourself("6");
player.checkyourself("7");
}//Main Close
}//Class close
我想计算用户的猜测。可以在这段代码中做到吗?
我该如何改进这段代码?代码运行正常,但我想计算用户猜测,是否可以在main方法中写入while(isAlive==true)
条件?
是否可以使用位置数组的长度来限制用户猜测?
答案 0 :(得分:0)
好吧,如果我理解你正确添加
numberofguess++;
在检查自己的方法将解决您的问题。