Java Switch 语句问题

标签 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;
    }     
  }
}

最佳答案

您需要在每个案例的末尾使用break。语法如下:

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

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

请告诉我这是否有帮助。

关于Java Switch 语句问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35533512/

相关文章:

java - 为什么宇宙飞船没有出现?

java - 将数组与 Switch 语句一起使用时需要常量表达式

swift - 从 ENUM 函数中调用外部类函数

javascript - 通过 Javascript 中的 switch() 语句使用数组

java - POST 调用接受 x-www-form-urlencoded 但拒绝 JSON

java - 如何在 Eclipse 和 Android Studio 之间共享 Java 源代码?

c# - 为什么这个 switch case 返回一个 double,即使它是一个 int?

ios - swift 4.2 枚举案例 ' ' 在类型 '?' 中找不到 switch 语句中的要求以冗余解包隐式解包的可选

java - TCLBLEND 失败 - Centos 6.4

java - 上传的文件未使用java在Google应用程序引擎中存储为blob