Hi everyone,
I'm new to java. I'm getting an error cannot find symbol m_weapon when i compare char which i assigned from math.random.
Thank you,

public class RPS
{
    public static void main (String[ ] args)
    {
        Scanner console = new Scanner(System.in); //used to gather user input
    
    
        System.out.println("WELCOME \n\n"+
           "This program simulates the Rock Paper Scissors game. \n" +
           "The rules are fairly simple. I randomly choose one weapon among Rock, Paper, and Scissors. \n" +
           "Then I invite you to choose your weapon.  \nYou will have to enter R for Rock, P for Paper, and S for Scissors. \n" +
           "The outcome of each battle is determined as follows : \nPaper wins against Rock, Rock wins against Scissors" +
           "and Scissors wins against Paper. \n\nWould you like to play with me? (Yes/No)");
           String intent;
           intent = console.next();
                   
         //write a while loop condition that will allow a user to play the game until he/she decides to stop
           while(intent.equalsIgnoreCase("Yes"))
           {
             System.out.println("Please enter a choice of weapon" + "P : Paper / R : Rock / S : Scissors");
              
             char weapon = console.next().charAt(0);
            
            
             
             int machine = (int)(Math.random() * 3 + 1);
             
                if (machine == 1)
                {
                 char m_weapon = 'P';                                  
                }
                else if(machine ==2)
                {
                 char m_weapon = 'R';
                 }
                else if(machine ==3 )
                {
                 char m_weapon = 'S';
                }                
                                  
                if (weapon == 'P' && weapon == 'R' && weapon == 'S')
                {
                 System.out.print("tie");                 
                }
                
                if(weapon == 'P' && m_weapon == 'R' || weapon =='R' && m_weapon == 'S'|| weapon =='S' && m_weapon =='P')              
                {
                    System.out.println("user wins");
                    
                }
                
               else if (weapon == 'R' && m_weapon == 'P' || weapon =='S' && m_weapon == 'R'|| weapon =='P' && m_weapon =='S')
                { 
                    System.out.println("machine wins");
                    
                }
            

                       } 
          
       
   
    }//closes main
}//closes the class

In java, curly braces { and } define what is known as scope. When you declare a variable, the nearest curly braces define the variable's scope, and the variable is not visible outside that scope. You have code that looks like this:

if (machine == 1)
  {
    char m_weapon = 'P';                                  
  }

You declare the variable m_weapon inside the scope of the if statement, and that variable cannot be seen outside of that scope. But you need to use the variable outside that scope, so you need to declare the variable at a higher level (outside those curly braces), like this:

char m_weapon;
if (machine == 1)
{
   m_weapon = 'P';                                  
}
else if(machine ==2)
{
   m_weapon = 'R';
}
... etc.

Now the variable m_weapon is visible outside of the if statement.

Thank you Kramerd, now it is giving variable m_weapon might not have been initilized error. any suggestions are appreciated.

That just means you need to initialize it to something when you declare it. For example,

char m_weapon = ' ';
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.