在switch语句中切换语句

时间:2014-02-27 04:17:29

标签: java input switch-statement

我正在尝试创建一个程序,使用switch语句在计算机上播放石头剪刀。我遇到的问题是该程序告诉我第二个switch语句(switch(user){)无法访问。任何提示都会有所帮助。

更新:修复了无法访问的问题!问题是switch语句不是读取个人计算机选择并输出所有。

Example Output:
Please enter an integer between 0 and 2 to play ( 0 = Rock 1 = Paper 2 = Scissors): 2
The computer chose Rock!
You chose Scissors! You loose!
The computer chose Paper!
You chose Scissors! You win!
The computer chose Scissors!
You chose Scissors! It's a draw

程序:

import java.util.Scanner;
public class RockPaperScissors
{
   public static void main(String[] args)
   {
      Scanner input = new Scanner(System.in);
      int comp = (int)Math.random()*3;
      System.out.print("Please enter an integer between 0 and 2 to play ( 0 = Rock 1 = Paper 2 = Scissors): ");
      int user = input.nextInt();

      while(user < 0) {
         System.out.print("Invalid integer, please type in a new number above zero and less than two: ");
         user = input.nextInt();
         }

      switch (comp){
         case 0: System.out.println("The computer chose Rock!"); break;
         switch (user){
            case 0: System.out.println("You chose Rock! It's a draw!"); break;
            case 1: System.out.println("You chose Paper! You win!"); break;
            case 2: System.out.println("You chose Scissors! You loose!"); break;
            }
         case 1: System.out.println("The computer chose Paper!"); break;
         switch (user){
            case 0: System.out.println("You chose Rock! You loose!"); break;
            case 1: System.out.println("You chose Paper! It's a draw!"); break;
            case 2: System.out.println("You chose Scissors! You win!"); break;
            }
         case 2: System.out.println("The computer chose Scissors!"); break;
         switch (user){
            case 0: System.out.println("You chose Rock! You win!"); break;
            case 1: System.out.println("You chose Paper! You loose!"); break;
            case 2: System.out.println("You chose Scissors! It's a draw"); break;
            }
         }

}
}      

3 个答案:

答案 0 :(得分:0)

在内部切换后移动break语句。

当编译器看到break

        case 0:
            System.out.println("The computer chose Rock!");
            switch (user) {
                case 0:
                    System.out.println("You chose Rock! It's a draw!");
                    break;
                case 1:
                    System.out.println("You chose Paper! You win!");
                    break;
                case 2:
                    System.out.println("You chose Scissors! You loose!");
                    break;
            }
         break;

同样适用于其他案例。中断或返回后的语句将不会被执行。 因此,编译器报告这些是无法访问的语句。

答案 1 :(得分:0)

您在break之前使用switch (user){,因此您的陈述是unreachable

试试这个:

switch (comp){
     case 0: System.out.println("The computer chose Rock!"); 
     switch (user){
        case 0: System.out.println("You chose Rock! It's a draw!"); break;
        case 1: System.out.println("You chose Paper! You win!"); break;
        case 2: System.out.println("You chose Scissors! You loose!"); break;
        }
     break;
     case 1: System.out.println("The computer chose Paper!"); 
     switch (user){
        case 0: System.out.println("You chose Rock! You loose!"); break;
        case 1: System.out.println("You chose Paper! It's a draw!"); break;
        case 2: System.out.println("You chose Scissors! You win!"); break;
        }
     break;
     case 2: System.out.println("The computer chose Scissors!");
     switch (user){
        case 0: System.out.println("You chose Rock! You win!"); break;
        case 1: System.out.println("You chose Paper! You loose!"); break;
        case 2: System.out.println("You chose Scissors! It's a draw"); break;
        }
     break;
     }

答案 2 :(得分:0)

@ user3358795:你在内部switch语句之前使用break语句这就是内部交换机无法访问的原因。

尝试以下代码:

switch (comp){
     case 0: System.out.println("The computer chose Rock!"); 
     switch (user){
        case 0: System.out.println("You chose Rock! It's a draw!"); break;
        case 1: System.out.println("You chose Paper! You win!"); break;
        case 2: System.out.println("You chose Scissors! You loose!"); break;
        }
     break;
     case 1: System.out.println("The computer chose Paper!"); 
     switch (user){
        case 0: System.out.println("You chose Rock! You loose!"); break;
        case 1: System.out.println("You chose Paper! It's a draw!"); break;
        case 2: System.out.println("You chose Scissors! You win!"); break;
        }
     break;
     case 2: System.out.println("The computer chose Scissors!");
     switch (user){
        case 0: System.out.println("You chose Rock! You win!"); break;
        case 1: System.out.println("You chose Paper! You loose!"); break;
        case 2: System.out.println("You chose Scissors! It's a draw"); break;
        }
     break;
     }