-2

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?

F. Müller
  • 3,969
  • 8
  • 38
  • 49
Hubert
  • 19
  • 4
  • 4
    "what's going on" please ask more specific question. We don't know which return statement you mean, nor what is *specifically* confusing you. Use [edit] option to clarify your question. – Pshemo Nov 15 '20 at 20:26
  • 1
    Which part don't you understand? `get(s1)`? `equals(s2)`? `format("Player %d wins",`? – Andreas Nov 15 '20 at 20:26
  • 2
    Recommend you draw a 3x3 grid, write "rock", "paper", and "scissors" down the left for player 1, and across the top for player 2. For each cell, follow the logic and fill in what the return value will be. Now you know what the code does. – Andreas Nov 15 '20 at 20:28
  • 1
    If you don’t understand what a method does, the first thing you should do is consult [its documentation](https://docs.oracle.com/en/java/javase/15/docs/api/java.base/java/lang/String.html#format(java.lang.String,java.lang.Object...)). If you pay attention to the **Parameters** section, you’ll notice a “format string” link which leads to a [full explanation](https://docs.oracle.com/en/java/javase/15/docs/api/java.base/java/util/Formatter.html#syntax) of what `%d` and other percent-substitutions are. – VGR Nov 16 '20 at 00:29

1 Answers1

1

If you are talking about the last return statement:

This is called String interpolation and you can read more about that here: Java - Including variables within strings?

But basically it means that you will write a whole sentence and you will put a tag inside the string such as %s or other to indicate the type of variable that will be there after formatting with String.format().

It allows you to avoid using + sign to separate strings and variables.

String aStringVar = "MyString";
int anIntVar = 5;
// Line below is the same as: "A string:" + aStringVar + "; and a number " + anIntVar + ";"
String interpolatedString = String.format("A String: %s; and a number %2d;", aStringVar, anIntVar);
System.out.println(interpolatedString);
// Console result: A String: MyString; and a number 5;
Corey
  • 94
  • 10
  • thanks, i have one more question. How does it work: rules.get(s1).equals(s2) ? 1 : 2 // i dont know how works this return method with hashmap and gives me right answer who will win this game – Hubert Nov 15 '20 at 20:58
  • 1
    @Hubert basically that part gets s1 and compares it to s2. It basically checks if hashmap has a match that you wrote in your code. For example if s1 = "paper" and s2 = "rock" then it is going to check if you have a match that you put in your code eg: rules.put("paper", "rock"); s1 is Paper and s2 is Rock and you did put that combination inside the hashmap. Therefore, when you check it with get.equals it returns true which means that s1 (paper) wins and rock losses. the part ? states to return 1 if value is true or to return 2 if value is false. Results: Player %d wins = Player 1 wins. s1 wins – Corey Nov 15 '20 at 21:15
  • 1
    @Hubert, please could you mark my answer as the best and put an upvote to my answers in case if that helped. – Corey Nov 15 '20 at 21:37