HW4.34

时间:2023-03-08 22:02:08

HW4.34

 import java.util.Scanner;

 public class Solution
 {
     public static void main(String[] args)
     {
         Scanner input = new Scanner(System.in);

         int playerWin = 0;
         int computerWin = 0;

         while(true)
         {

             if(playerWin == 2 || computerWin == 2)
                 break;

             System.out.print("scissor(0), rock(1), paper(2): ");
             int playerValue = input.nextInt();

             int computerValue = (int)(Math.random() * 3);

             String player = "";
             if(playerValue == 0)
                 player = "scissor";
             else if(playerValue == 1)
                 player = "rock";
             else if(playerValue == 2)
                 player = "paper";

             String computer = "";
             if(computerValue == 0)
                 computer = "scissor";
             else if(computerValue == 1)
                 computer = "rock";
             else if(computerValue == 2)
                 computer = "paper";

             if(playerValue == computerValue)
                 System.out.println("The computer is " + computer + ". You are " + player + " too. It is a draw.");
             else
                 System.out.print("The computer is " + computer + ". You are " + player + ". ");

             if(playerValue == 0 && computerValue == 1 || playerValue == 1 && computerValue == 2 ||
                 playerValue == 2 && computerValue == 0)
             {
                 System.out.println("Computer win");
                 computerWin++;
             }
             if(playerValue == 1 && computerValue == 0 || playerValue == 2 && computerValue == 1 ||
                 playerValue == 0 && computerValue == 2)
             {
                 System.out.println("You win");
                 playerWin++;
             }
         }

         input.close();
     }
 }