binibiningtinamoran

ROCKPAPERSCISSORS

May 25th, 2019
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.12 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class RockPaperScissors {
  4.  
  5.     public static final String ROCK = "R";
  6.     public static final String PAPER = "P";
  7.     public static final String SCISSORS = "S";
  8.  
  9.     public static void main(String []args) {
  10.         Scanner input = new Scanner (System.in);
  11.  
  12.         System.out.print("Enter number of games: ");
  13.         int numberOfGames = input.nextInt();
  14.  
  15.         while (numberOfGames < 1) {
  16.             System.out.println("Invalid number of games.");
  17.             System.out.print("Enter number of games: ");
  18.             numberOfGames = input.nextInt();
  19.         }
  20.  
  21.         int rounds = 1;
  22.         int player1Score = 0;
  23.         int player2Score = 0;
  24.         System.out.print("Enter name of Player 1: ");
  25.         String player1 = input.next();
  26.  
  27.         System.out.print("Enter name of Player 2: ");
  28.         String player2 = input.next();
  29.  
  30.         do {
  31.             System.out.printf("Round %,d", rounds);
  32.             System.out.printf("\nWhat does %s play? R,P or S: ", player1);
  33.             String player1Choice = input.next().toUpperCase();
  34.  
  35.             while (!(player1Choice.equals(ROCK) || player1Choice.equals(PAPER) || player1Choice.equals(
  36.                     SCISSORS))) {
  37.                 System.out.println("Invalid choice made. Please select only R, P, or S.");
  38.                 System.out.printf("What does %s play? R,P or S: ", player1);
  39.                 player1Choice = input.next().toUpperCase();
  40.             }
  41.  
  42.             System.out.printf("What does %s play? R,P or S: ", player2);
  43.             String player2Choice = input.next().toUpperCase();
  44.  
  45.             while (!(player2Choice.equals(ROCK) || player2Choice.equals(PAPER) || player2Choice.equals(
  46.                     SCISSORS))) {
  47.                 System.out.println("Invalid choice made. Please select only R, P, or S.");
  48.                 System.out.printf("What does %s play? R,P or S: ", player2);
  49.                 player2Choice = input.next().toUpperCase();
  50.             }
  51.  
  52.             if (player1Choice.equals(player2Choice)) {
  53.                 System.out.println("It's a tie.");
  54.             } else if ((player1Choice.equals(ROCK) && player2Choice.equals(SCISSORS)) ||
  55.                     (player1Choice.equals(SCISSORS) && player2Choice.equals(PAPER)) ||
  56.                     (player1Choice.equals(PAPER) && player2Choice.equals(ROCK))) {
  57.                 System.out.printf("%s wins!\n", player1);
  58.                 player1Score++;
  59.             } else {
  60.                 assert ((player2Choice.equals(ROCK) && player1Choice.equals(SCISSORS)) ||
  61.                         (player2Choice.equals(SCISSORS) && player1Choice.equals(PAPER)) ||
  62.                         (player2Choice.equals(PAPER) && player1Choice.equals(ROCK)));
  63.                 System.out.printf("%s wins!\n", player2);
  64.                 player2Score++;
  65.             }
  66.  
  67.             rounds++;
  68.  
  69.         } while(rounds <= numberOfGames);
  70.  
  71.         System.out.println();
  72.         System.out.printf("After %,d rounds, ", numberOfGames);
  73.         System.out.println(player1Score>player2Score ? player1 + " wins!" : player2 +
  74.                  " wins!");
  75.     } // end main
  76.  
  77. }
Advertisement
Add Comment
Please, Sign In to add comment