石头剪刀布猜谜

猜一次

  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 @   Scenery_Shelley  阅读(29)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示