Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class RockPaperScissors {
- public static final String ROCK = "R";
- public static final String PAPER = "P";
- public static final String SCISSORS = "S";
- public static void main(String []args) {
- Scanner input = new Scanner (System.in);
- System.out.print("Enter number of games: ");
- int numberOfGames = input.nextInt();
- while (numberOfGames < 1) {
- System.out.println("Invalid number of games.");
- System.out.print("Enter number of games: ");
- numberOfGames = input.nextInt();
- }
- int rounds = 1;
- int player1Score = 0;
- int player2Score = 0;
- System.out.print("Enter name of Player 1: ");
- String player1 = input.next();
- System.out.print("Enter name of Player 2: ");
- String player2 = input.next();
- do {
- System.out.printf("Round %,d", rounds);
- System.out.printf("\nWhat does %s play? R,P or S: ", player1);
- String player1Choice = input.next().toUpperCase();
- while (!(player1Choice.equals(ROCK) || player1Choice.equals(PAPER) || player1Choice.equals(
- SCISSORS))) {
- System.out.println("Invalid choice made. Please select only R, P, or S.");
- System.out.printf("What does %s play? R,P or S: ", player1);
- player1Choice = input.next().toUpperCase();
- }
- System.out.printf("What does %s play? R,P or S: ", player2);
- String player2Choice = input.next().toUpperCase();
- while (!(player2Choice.equals(ROCK) || player2Choice.equals(PAPER) || player2Choice.equals(
- SCISSORS))) {
- System.out.println("Invalid choice made. Please select only R, P, or S.");
- System.out.printf("What does %s play? R,P or S: ", player2);
- player2Choice = input.next().toUpperCase();
- }
- if (player1Choice.equals(player2Choice)) {
- System.out.println("It's a tie.");
- } else if ((player1Choice.equals(ROCK) && player2Choice.equals(SCISSORS)) ||
- (player1Choice.equals(SCISSORS) && player2Choice.equals(PAPER)) ||
- (player1Choice.equals(PAPER) && player2Choice.equals(ROCK))) {
- System.out.printf("%s wins!\n", player1);
- player1Score++;
- } else {
- assert ((player2Choice.equals(ROCK) && player1Choice.equals(SCISSORS)) ||
- (player2Choice.equals(SCISSORS) && player1Choice.equals(PAPER)) ||
- (player2Choice.equals(PAPER) && player1Choice.equals(ROCK)));
- System.out.printf("%s wins!\n", player2);
- player2Score++;
- }
- rounds++;
- } while(rounds <= numberOfGames);
- System.out.println();
- System.out.printf("After %,d rounds, ", numberOfGames);
- System.out.println(player1Score>player2Score ? player1 + " wins!" : player2 +
- " wins!");
- } // end main
- }
Advertisement
Add Comment
Please, Sign In to add comment