Может кто-нибудь помочь мне превратить расширенный цикл for for(int cell:locationCells)
в обычный цикл for?И почему в коде break;
?Спасибо!
public class SimpleDotCom {
int[] locationCells;
int numOfHits = 0 ;
public void setLocationCells(int[] locs){
locationCells = locs;
}
public String checkYourself(int stringGuess){
int guess = stringGuess;
String result = "miss";
for(int cell:locationCells){
if(guess==cell){
result ="hit";
numOfHits++;
break;
}
}
if(numOfHits == locationCells.length){
result ="kill";
}
System.out.println(result);
return result;
}
}
public class main {
public static void main(String[] args) {
int counter=1;
SimpleDotCom dot = new SimpleDotCom();
int randomNum = (int)(Math.random()*10);
int[] locations = {randomNum,randomNum+1,randomNum+2};
dot.setLocationCells(locations);
boolean isAlive = true;
while(isAlive == true){
System.out.println("attempt #: " + counter);
int guess = (int) (Math.random()*10);
String result = dot.checkYourself(guess);
counter++;
if(result.equals("kill")){
isAlive= false;
System.out.println("attempt #" + counter);
}
}
}
}