2016-02-21 3 views
0

первый раз постер и новичок кодер. Я прошу прощения за беспорядок этого кода, все еще нуждается в очистке. Однако у меня возникают серьезные проблемы с моим коммутатором. Если я выберем случай 1, он пробежит случай 1, а после завершения автоматически пробежит случай 2, затем случай 3. Аналогично, если я выберем случай 2, то после завершения дела 2 он пробежит случай 3. Наконец, мой дефолт также не работает. При выборе ничего, кроме 1, 2 или 3, появляются всевозможные ошибки. Любая помощь будет принята с благодарностью!Java Switch-Statement Troubles

Если вам сложно найти эту сложную проблему!

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; 
    . 
    . 
    . 
} 

Сообщите мне, если это поможет.