1. 程式人生 > 其它 >石頭剪刀布猜謎

石頭剪刀布猜謎

  import java.util.*;
  class Main {
    public static void main(String[] args){
      Scanner input = new Scanner(System.in);
      System.out.print("scissor(0), rock(1), paper(2): ");
      
      int userResult = input.nextInt();
      int computerResult = 0 + (int)(Math.random() * 10) % 2;

      String userResultString = "";
      String computerResultString = "";
      
      switch(userResult){
        case 0:
          userResultString = "scissor";
          break;
        case 1:
          userResultString = "rock";
          break;
        case 2:
          userResultString = "paper";
          break;
     }
      
      switch(computerResult){
        case 0:
          computerResultString = "scissor";
          break;
        case 1:
          computerResultString = "rock";
          break;
        case 2:
          computerResultString = "paper";
          break;
     }
      if(userResult == computerResult){
        System.out.print("The computer is " + computerResultString + ". You are " + 
                        userResultString + " too. It is a draw");
      }else if(userResult == 0 && computerResult == 1){
        System.out.print("The computer is " + computerResultString + ". You are " + 
                        userResultString + ". You lose");
      }else if(userResult == 0 && computerResult == 2){
        System.out.print("The computer is " + computerResultString + ". You are " + 
                        userResultString + ". You win");
      }else if(userResult == 1 && computerResult == 0){
        System.out.print("The computer is " + computerResultString + ". You are " + 
                        userResultString + ". You win");
      }else if(userResult == 1 && computerResult == 2){
        System.out.print("The computer is " + computerResultString + ". You are " + 
                        userResultString + ". You lose");
      }else if(userResult == 2 && computerResult == 1){
        System.out.print("The computer is " + computerResultString + ". You are " + 
                        userResultString + ". You win");
      }else if(userResult == 2 && computerResult == 0){
        System.out.print("The computer is " + computerResultString + ". You are " + 
                        userResultString + ". You lose");
      }
      
    }
  }