cgorrillaha

Untitled

Oct 19th, 2021
954
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.53 KB | None | 0 0
  1. package Unit3;
  2. import java.util.Scanner;
  3. public class Rock {
  4.     public static void main(String[] args) {
  5.         //declarations
  6.         int cpuRand;
  7.         String cpuPlay="", userPlay;
  8.         Scanner s=new Scanner(System.in);
  9.         boolean playerWin=false;
  10.  
  11.         //inputs
  12.         cpuRand=(int)(Math.random()*3)+1;//gen 1 - 3
  13.         System.out.println("Please enter your play: R, P, S");
  14.         userPlay=s.next();
  15.  
  16.         //computations
  17.         //assign cpuPlay
  18.         switch(cpuRand){
  19.             case 1:
  20.                 cpuPlay="R";
  21.                 break;
  22.             case 2:
  23.                 cpuPlay="P";
  24.                 break;
  25.             case 3:
  26.                 cpuPlay="S";
  27.                 break;
  28.             default:
  29.                 cpuPlay="R";
  30.         }
  31.         //resolve round
  32.         if(userPlay.equals("R")){
  33.             if(cpuPlay.equals("S")){
  34.                 playerWin=true;
  35.             }
  36.         }else if(userPlay.equals("P")){
  37.             if(cpuPlay.equals("R")){
  38.                 playerWin=true;
  39.             }
  40.         }else{//userPlay == "S"
  41.             if(cpuPlay.equals("P")){
  42.                 playerWin=true;
  43.             }
  44.         }
  45.  
  46.         System.out.printf("You played %s%n", userPlay);
  47.         System.out.printf("The compter played %s%n", cpuPlay);
  48.  
  49.         if(userPlay.equals(cpuPlay)){
  50.             System.out.println("Tie");
  51.         }else if(playerWin){
  52.             System.out.println("You win!");
  53.         }else{
  54.             System.out.println("You lose!");
  55.         }
  56.  
  57.     }
  58. }
  59.  
Advertisement
Add Comment
Please, Sign In to add comment