public class SimpleDotComTestDrive {
public static void main(String[] args) {
SimpleDotCom dot = new SimpleDotCom();
int[] locations = {2,3,4};
dot.setLocationCells(locations);
String userGuess = "2";
String result = dot.checkYourself(userGuess);//==result==hit
userGuess = "3";
result = dot.checkYourself(userGuess);
userGuess = "4";
result = dot.checkYourself(userGuess);
}
}
class SimpleDotCom{
int [] locationCells;
int numOfHits = 0;
public void setLocationCells (int[] locs) {
locationCells = locs; // {2,3,4}
}
public String checkYourself(String stringGuess) {
int guess = Integer.parseInt(stringGuess);
String result = "miss";
for(int cell : locationCells) {
if(guess== cell) {
result = "hit";
numOfHits++;
System.out.println(numOfHits);
break;
}
}
System.out.println(result);
if (numOfHits == locationCells.length) {
result = "kill";
System.out.println(result);
}
return result;
}
}
return result;
in the above codeI'm not sure what it means.
The result value in SimpleDotComTestDrive is
I learned that there is no return value because SimpleDotComTestDrive class has void value.
I'm really curious about the reason for the return result.
java return
I think it's an incomplete code. And SimpleDotCom's checkYourself method returns, but I don't think it matters if SimpleDotComTestDrive doesn't have a return value.
return result;
is
Returns the execution result of the method, which means contained in the result.
That is, the execution result value of the checkYourself method in the SimpleDotCom class, and you called it here.
String result = dot.checkYourself(userGuess);//==result==hit
To be exact, the SimpleDotComTestDrive class does not have a void value, but the main method with the return type void is defined in the SimpleDotComTestDrive class.
1. public class SimpleDotComTestDrive {
2.
3. public static void main(String[] args) {
4. SimpleDotCom dot = new SimpleDotCom();
5. int[] locations = {2,3,4};
6. dot.setLocationCells(locations);
7. String userGuess = "2";
8. String result = dot.checkYourself(userGuess);//==result==hit
9. userGuess = "3";
10. result = dot.checkYourself(userGuess);
11. userGuess = "4";
12. result = dot.checkYourself(userGuess);
13. }
14. }
class SimpleDotCom{
int [] locationCells;
int numOfHits = 0;
public void setLocationCells (int[] locs) {
locationCells = locs; // {2,3,4}
}
public String checkYourself(String stringGuess) {
int guess = Integer.parseInt(stringGuess);
String result = "miss";
for(int cell : locationCells) {
if(guess== cell) {
result = "hit";
numOfHits++;
System.out.println(numOfHits);
break;
}
}
System.out.println(result);
if (numOfHits == locationCells.length) {
result = "kill";
System.out.println(result);
}
return result;
}
}
If you look at the code above and follow it from the beginning
Third line
SimpleDotCom dot = new SimpleDotCom();
In , create a Simple DotCom Class as New.
And the return result you're curious about is
8.In the first line
String result = dot.checkYourself(userGuess);//==result==hit
Called dot, the checkYourself method for the class you created above.
If you look at the check your self method
public String checkYourself(String stringGuess) {
int guess = Integer.parseInt(stringGuess);
String result = "miss";
for(int cell : locationCells) {
if(guess== cell) {
result = "hit";
numOfHits++;
System.out.println(numOfHits);
break;
}
}
System.out.println(result);
if (numOfHits == locationCells.length) {
result = "kill";
System.out.println(result);
}
return result;
}
You can see that the string defines the return.
So check Yourself
return result;
That is, the result variable of String is Return to the main method result variable in the SimpleDotComTestDrive class.
© 2024 OneMinuteCode. All rights reserved.