石头剪刀布猜谜

猜一次

  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");
      }
      
    }
  }

猜到用户或者计算机赢两次结束


  import java.util.*;
  class Main {
    public static void main(String[] args){
      Scanner input = new Scanner(System.in);

      int countUserWin = 0;
      int countComputerWin = 0;
      while(countUserWin < 2 && countComputerWin < 2){
        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");
          countComputerWin++; 
        }else if(userResult == 0 && computerResult == 2){
          System.out.print("The computer is " + computerResultString + ". You are " + 
                          userResultString + ". You win");
          countUserWin++;
        }else if(userResult == 1 && computerResult == 0){
          System.out.print("The computer is " + computerResultString + ". You are " + 
                          userResultString + ". You win");
          countUserWin++;
        }else if(userResult == 1 && computerResult == 2){
          System.out.print("The computer is " + computerResultString + ". You are " + 
                          userResultString + ". You lose");
          countComputerWin++;
        }else if(userResult == 2 && computerResult == 1){
          System.out.print("The computer is " + computerResultString + ". You are " + 
                          userResultString + ". You win");
          countUserWin++;
        }else if(userResult == 2 && computerResult == 0){
          System.out.print("The computer is " + computerResultString + ". You are " + 
                          userResultString + ". You lose");
          countComputerWin++;
        }
      }
      input.close();
    }
  }
posted @ 2022-05-13 11:37  Scenery_Shelley  阅读(34)  评论(0)    收藏  举报