Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Java Rock-Paper-Scissors game not working

This game I made has some problems on it that I can’t solve. It’s a game that you should always lose. But it only shows the first menu, and whatever you enter, nothing happens.

Can you help me fix it?

First menu I am talking about: First Menu

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The code:

import javax.swing.JOptionPane; 

public class mainWork {  

public static void main(String[] args) { 
    int restart = 0; 
    String wannaplay = "no"; 
    
    int pc = 0;
    int user = 0;
    
    while(restart == 0) {
        String op = JOptionPane.showInputDialog("Which move you wanna do? \n(Rock, Paper, Scissors) \nUser:" + user + "\nPC:" + pc);
        String useropinion = op.toLowerCase(); 
        
        if(useropinion == "rock") { 
            pc = pc + 1; //pc skoruna bir eklemesi için
            JOptionPane.showMessageDialog(null, "PC has choosen Paper, you lost!");
            wannaplay = JOptionPane.showInputDialog("Do you wanna play again?"); 
            wannaplay = wannaplay.toLowerCase(); 
            
            if (wannaplay == "yes") { 
                restart = 0;
            } 
            else {
                restart = 1;
            }
            
        if(useropinion == "paper") {
            pc = pc + 1;
            JOptionPane.showMessageDialog(null, "PC has choosen Scissors, you lost!");
            wannaplay = JOptionPane.showInputDialog("Do you wanna play again?");
            wannaplay = wannaplay.toLowerCase();
                
            if (wannaplay == "yes") {
                restart = 0;
            } 
            else {
                restart = 1;
            }
        }
        
        if(useropinion == "rock") {
            pc = pc + 1;
            JOptionPane.showMessageDialog(null, "PC has choosen Paper, you lost!");
            wannaplay = JOptionPane.showInputDialog("Do you wanna play again?");
            wannaplay = wannaplay.toLowerCase();
                
            if (wannaplay == "yes") {
                restart = 0;
            } 
            else {
                restart = 1;
            }
         }
      }
   }
}}

>Solution :

You have to compare strings using the .equals() method

So replace all the

useropinion == "something"

with

"something".equals(useropinion)

See: How do I compare strings in Java? for more info

The reason "foo".equals(variable) is because the string literal won’t be null, while variable.equals("foo") can throw NullPointerException.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading