Java Switch-Statement麻烦

时间:2016-02-21 07:35:08

标签: java switch-statement

第一次海报和新手编码器。我为这段代码的混乱道歉,仍然需要清理。但是,我的交换机出现了重大问题。如果我选择案例1,它将通过案例1,然后在完成后,自动运行案例2,然后案例3.同样,如果我选择案例2,在案例2完成后,它将贯穿案例3.最后,我的默认也没有工作。选择除1,2或3以外的任何内容时,会出现各种错误。任何帮助将不胜感激!

很难找到这个复杂的问题!

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 个答案:

答案 0 :(得分:1)

你需要在每个案例结束时使用break。语法如下:

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

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

如果有帮助,请告诉我。