0

first time poster, and newbie coder. I apologize for the mess of this code, still needs cleaning. However, I am having major issues with my switch. If I select case 1, it runs through case 1, then after completion, automatically runs through case 2, then case 3. Likewise, if I select case 2, after completion of case 2, it runs through case 3. Finally, my default isn't working either. When selecting anything other than 1, 2, or 3, all sorts of errors appear. Any help would be greatly appreciated!

Had a hard time searching for this complex issue!

switch(input3)
{
  case 1 :
  {  
    JOptionPane.showMessageDialog(null,"You have selected the Rock, Paper, Scissors Game\nThe program is now loading...\nProgram loaded successfully!  Please press OK.");
       JOptionPane.showMessageDialog(null,"Rock, Paper, Scissors Game... 3 Rounds!");
       while(round<3) { 
       String UserInput = JOptionPane.showInputDialog("Rock, Paper, Scissors Game: Type Rock, Paper, or Scissors");
       Random Game = new Random();
       
                int Computer = 1+Game.nextInt(3);

                int Scissors, Rock, Paper;
                Rock = 1;
                Paper = 2;
                Scissors= 3;
           
                if(UserInput.equals("Rock"))  {
                    Player = 1;
                }
                if(UserInput.equals("Paper")) {
                    Player = 2;
                }
                if(UserInput.equals("Scissors")) {
                    Player = 3;
                }
 int random = (int)(Math.random() * 3);
  String computerInput; // Computer's choice
  if (random == 0) 
   computerInput = "rock";
  else if (random == 1)
   computerInput = "paper";
  else
   computerInput = "scissor";
  
while (Player > 3 || Player < 1) {
                    losses++;
                    round++;
                    System.out.println("Not a valid input! Computer wins");
                    System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");

                }
  
   //Establish tie scenarios using if statements
                if(UserInput== computerInput){
                  JOptionPane.showMessageDialog(null,"Player has won " + wins + " times and the computer has won " + losses + " times");
               round++;
                }
  
   //Winning scenerios             
                if(Player == Scissors)
                    if(Computer == Paper){
                        JOptionPane.showMessageDialog(null,"Scissors v Paper! Player Wins!");
                        wins++;
                        round++;
                        JOptionPane.showMessageDialog(null,"Player has won " + wins + " times and the computer has won " + losses + " times");
                    }
                
                  //Computer wins
                    else if(Computer == Rock){
                        JOptionPane.showMessageDialog(null,"Scissors v Rock! Computer Wins!");
                        losses++;
                        round++;
                        JOptionPane.showMessageDialog(null,"Player has won " + wins + " times and the computer has won " + losses + " times");
                    }
                //Player wins
                if(Player == Rock)
                    if(Computer == Scissors ){
                        JOptionPane.showMessageDialog(null,"Rock v Scissors! Player Wins!");
                        wins++;
                        round++;
                        JOptionPane.showMessageDialog(null,"Player has won " + wins + " times and the computer has won " + losses + " times");
                    }   
                //Computer wins
                    else if (Computer == Paper){
                        JOptionPane.showMessageDialog(null,"Rock v Paper! Computer Wins!");
                        losses++;
                        round++;
                        JOptionPane.showMessageDialog(null,"Player has won " + wins + " times and the computer has won " + losses + " times");
                    }
                //Player Wins
                if(Player == Paper)
                    if(Computer == Rock){
                        JOptionPane.showMessageDialog(null,"Paper v Rock! Player Wins!");
                        wins++;
                        round++;
                        JOptionPane.showMessageDialog(null,"Player has won " + wins + " times and the computer has won " + losses + " times");

                    }
                //Computer Wins 
                    else if (Computer == Scissors){
                        JOptionPane.showMessageDialog(null,"Paper v Scissors! Computer Wins!");
                        losses++;
                        round++;
                        JOptionPane.showMessageDialog(null,"Player has won " + wins + " times and the computer has won " + losses + " times");
                    }
       }
                    if(wins>losses){
                JOptionPane.showMessageDialog(null,"The Player Wins!");
            }if(losses>wins){
                JOptionPane.showMessageDialog(null,"The Computer Wins!");
  }
  }
    case 2 :
    {
        JOptionPane.showMessageDialog(null,"You have selected the game's rules.\nOpening the rules...\nLoaded successfully!  Please press OK.");
        JOptionPane.showMessageDialog(null,"Here are the rules for the game:\nIn total, there are 3 rounds for this game, including Ties.\nPaper vs Rock => Paper is the Winner.  Add 1 to the winner.\nPaper vs Scissors => Scissors is the Winner.  Add 1 to the winner.\nRock vs Scissors => Rock is the Winner.  Add 1 to the winner.\nRock vs Rock => Tie.  No score.\nScissors vs Scissors => Ties.  No score.\nPaper vs Paper => Tie.  No score.");
    }

    case 3 :
    {
        JOptionPane.showMessageDialog(null,"3");
    }
    default :
      JOptionPane.showMessageDialog(null,"You entered an invalid operation.  Please select 1, 2, or 3.");
        
    String input2 = JOptionPane.showInputDialog("Would you like to return to the main menu?  Please respond with Yes or No.");
    if (input2.equalsIgnoreCase ("No"))
    JOptionPane.showMessageDialog(null,"You have selected to exit the program.  Closing the program... please press OK.");
    {
    i=1;
    }     
  }
}

1 Answers1

1

You need to use break at the end of every case. Syntax is like:

switch(variable){
    case 1:
        //Do your operations.
    break;

    case 2:
        //Do your operations.
    break;
    .
    .
    .
}

Let me know if this helps.

Alok Gupta
  • 1,353
  • 1
  • 13
  • 29