Please could anyone explain what's going on with the return method(String.format)?
Also, I would like to know how does the code inside the last return statement works?
import java.util.Map;
import java.util.HashMap;
public class RockPaperScissors {
public static void main(String []args) {
String result = RockPaperScissors("paper","rock");
System.out.println(result);
}
public static String RockPaperScissors(String player1, String player2) {
Map<String, String> rules = new HashMap<>();
rules.put("rock", "scissors");
rules.put("paper", "rock");
rules.put("scissors", "paper");
if (player1.equals(player2)) {
return "TIE";
}
return String.format("Player %d wins", rules.get(player1).equals(player2) ? 1 : 2);
}
}
The part that I do not understand is this:
String.format("Player %d wins", rules.get(player1).equals(player2) ? 1 : 2);
What does it do and what is the meaning of %d
inside of it?