Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ca.softwarebyslim.filefilter;
- import java.util.Random;
- import javax.swing.*;
- public class RPS
- {
- public static void main(String[] args)
- {
- // Declare variables.
- String choiceString;
- String playAgain;
- Random r = new Random();
- int win = 0;
- int tie = 0;
- int lose = 0;
- int round = 0;
- double percent = 0;
- //Convert Choice string to phrase for println
- //While Loop
- playAgain = JOptionPane.showInputDialog( "Would you like to play RPS?");
- while(playAgain.equalsIgnoreCase ("Y")){
- do
- {
- round++;
- System.out.println("Round " + (round));
- choiceString = JOptionPane.showInputDialog("Choose your throw, (R) for rock, (P) for paper, or (S) for scissors. You can type (Q) to exit and get the final score." );
- int compChoice = r.nextInt(3);
- if(choiceString.equalsIgnoreCase ("R"))
- {
- System.out.print(" You Chose Rock "); //User's Choice
- if(compChoice == 0)
- {
- System.out.println(" I Chose Rock as well. We tie.");
- tie++;
- }
- if(compChoice == 1)
- {
- System.out.println(" I Chose Paper. You lose.");
- lose++;
- }
- if(compChoice == 2)
- {
- System.out.println(" I Chose Scissors. You win!");
- win++;
- }
- }
- else if(choiceString.equalsIgnoreCase ("P"))
- {
- System.out.print(" You Chose Paper "); //User's Choice
- if(compChoice == 0)
- {
- System.out.println(" I Choose Rock. You win!");
- win++;
- }
- if(compChoice == 1)
- {
- System.out.println(" I Choose Paper As Well. We tie.");
- tie++;
- }
- if(compChoice == 2)
- {
- System.out.println(" I Choose: Scissors. You lose.");
- lose++;
- }
- }
- else if(choiceString.equalsIgnoreCase("S"))
- {
- System.out.print(" You Chose Scissors "); //User's Choice
- if(compChoice == 0)
- {
- System.out.println(" I Choose: Rock. You lose.");
- lose++;
- }
- if(compChoice == 1)
- {
- System.out.println(" I Choose: Paper. You win!");
- win++;
- }
- if(compChoice == 2)
- {
- System.out.println(" I Choose: Scissors as well. We tie.");
- tie++;
- }
- }
- else if(choiceString.equalsIgnoreCase("Q"))
- {
- System.out.println(" Game Over.");
- playAgain = "N";
- break;
- }
- else
- {
- System.out.println(" Remember (R) for rock, (P) for paper, or (S) for scissors. You can type (Q) to exit. ");
- }
- }
- while(true);
- System.out.println("We played " + round + " rounds------- You Won " + win + " times------- You Lost " + lose + " times------- We Tied " + tie + " times-------");
- percent = (win*100/round);
- System.out.println(" You win percentage is " + percent + "%");
- playAgain = JOptionPane.showInputDialog( "Would you like to play RPS?");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement