2016-02-21 72 views
0

第一次海報和新手編碼器。我爲這段代碼的混亂道歉,仍然需要清理。但是,我的交換機存在重大問題。如果我選擇第一種情況,它將在案例1中運行,然後在完成後自動遍歷案例2,然後是案例3.同樣,如果我選擇案例2,則在案例2完成後,它將遍歷案例3.最後,我的默認也沒有工作。當選擇1,2或3以外的任何內容時,會出現各種錯誤。任何幫助將不勝感激!Java交換語句錯誤

很難找到這個複雜的問題!

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

你必須在每一種情況下結束使用休息。語法如下:

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

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

讓我知道這是否有幫助。