Advertisement
Mhazard

Java Rock Paper Scissors

Oct 24th, 2015
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.88 KB | None | 0 0
  1. // 15101194S
  2.  
  3. //Import Scanner
  4. import java.util.Scanner;
  5. import java.util.Random;
  6. public class PaperScissorsRock
  7. {
  8.         public static void main(String[] args)
  9.         {
  10.              //Title
  11.              System.out.println("Rock, Paper, Scissors");
  12.              //Scanner
  13.              Scanner sc = new Scanner(System.in);
  14.              //Insert Values
  15.              System.out.print("Enter 0 for paper, 1 for scissors, or 2 for rock (-1 to quit): ");
  16.              int Value1 = sc.nextInt();
  17.              //Randomizer
  18.              Random randomGen = new Random();
  19.              // nextInt() returns a pseudo random, uniformly
  20.              // distributed integer value between 0 (inclusive)
  21.              // and the specified value (exclusive), therefore,
  22.              // the following generates a random value between 0 to 2
  23.              int Value2 = randomGen.nextInt(3);
  24.              //Computer Output
  25.              switch(Value2)
  26.              {
  27.              case 0:
  28.                 System.out.println("Computer picks paper");
  29.                 break;
  30.              case 1:
  31.                 System.out.println("Computer picks scissors");
  32.                 break;
  33.              case 2:
  34.                 System.out.println("Computer picks rock");
  35.                 break;
  36.              }
  37.              //Calculation
  38.              if (Value1 >= 0){
  39.                 if ((Value1 == 0 && Value2 == 2) || (Value1 == 1 && Value2 == 0) || (Value1 == 2 && Value2 == 1))
  40.                       System.out.println("Player Wins");
  41.                 if ((Value1 == 0 && Value2 == 0) || (Value1 == 1 && Value2 == 1) || (Value1 == 2 && Value2 == 2))
  42.                     System.out.println("Draw");  
  43.              else
  44.              System.out.println("Computer Wins");
  45.              }
  46.          //Error
  47.          if (Value1 < 0)
  48.                  System.out.println("Invalid input.");
  49.              //Output
  50.          }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement