0

So I tried to make a rock paper scissor game in Java. Everything went smooth, but there's a little bit problem in my code, check this out...

import java.util.*;

public class App {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        Boolean play = true;
        int skorPemain = 0, skorKomp = 0;

        while (play == true) {
            int random = new Random().nextInt(3);
            String komp, pemain = "";

            if (random == 0) {
                komp = "Gunting";
            } else if (random == 1) {
                komp = "Batu";
            } else {
                komp = "Kertas";
            }

            System.out.print("Game Suit :\n"
                    + "1. Gunting\n"
                    + "2. Batu\n"
                    + "3. Kertas\n"
                    + "Pilih [1/2/3] : ");
            int pilih = input.nextInt();

            switch (pilih) {
                case 1:
                    pemain = "Gunting";
                    break;
                case 2:
                    pemain = "Batu";
                    break;
                case 3:
                    pemain = "Kertas";
                    break;
            }

            if (pemain == "Gunting" && komp == "Kertas" ||
                    pemain == "Batu" && komp == "Gunting" ||
                    pemain == "Kertas" && komp == "Batu") {
                System.out.println("Pemain memilih " + pemain + " dan komputer memilih " + komp
                        + "\nPemain menang, karena " + pemain + " mengalahkan " + komp);
                skorPemain++;
            } else {
                System.out.println("Pemain memilih " + pemain + " dan komputer memilih " + komp
                        + "\nKomputer menang, karena " + komp + " mengalahkan " + pemain);
                skorKomp++;
            }
            System.out.println("\nSkor Pemain vs Komputer : " + skorPemain + " vs " + skorKomp);

            System.out.print("Mau main lagi [y/n] : ");
            String playAgain = input.next();
            System.out.println("");

            if (playAgain == "n") {
                System.out.println("Terima kasih sudah bermain.");
                play = false;
            } else if (playAgain == "y") {
                continue;
            } else {
                System.out.println("Wrong input!");
                play = false;
            }
        }
    }
}

I run the code, when I input "n" in the terminal, I expected to get

"Terima kasih sudah bermain."

but instead I get the output

"Wrong input!".

It seems like there is a little problem in the if statement or maybe in the while statement, idk. Please help me fix it...

  • always use .equals to compare string – Icarus Nov 03 '22 at 08:51
  • Even comparisons like `pemain == "Gunting"` are bound to fail. In your case it's probably working because the literals are interned and refer to the very same instance but you can't depend on that. _Never_ compare strings using `==` unless you really want the same instance (not just the same value). Use `equals(...)` instead, e.g. `"Gunting".equals(pemain)` to also catch `pemain` potentially being `null`. – Thomas Nov 03 '22 at 08:53

0 Answers0