Advertisement
Guest User

Untitled

a guest
May 7th, 2013
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.33 KB | None | 0 0
  1. package ca.softwarebyslim.filefilter;
  2.  
  3. import java.util.Random;
  4.  
  5. import javax.swing.*;
  6.  
  7. public class RPS
  8. {
  9.         public static void main(String[] args)
  10.         {
  11.                 // Declare variables.
  12.                 String choiceString;
  13.                 String playAgain;
  14.                 Random r = new Random();
  15.                 int win = 0;
  16.                 int tie = 0;
  17.                 int lose = 0;
  18.                 int round = 0;
  19.                 double percent = 0;
  20.                 //Convert Choice string to phrase for println
  21.                
  22.                
  23.                 //While Loop
  24.                 playAgain = JOptionPane.showInputDialog( "Would you like to play RPS?");
  25.                
  26.                 while(playAgain.equalsIgnoreCase ("Y")){
  27.                
  28.                
  29.                     do
  30.                     {
  31.                             round++;
  32.                             System.out.println("Round " + (round));
  33.                             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." );
  34.                             int compChoice = r.nextInt(3);
  35.                            
  36.                             if(choiceString.equalsIgnoreCase ("R"))
  37.                            
  38.                             {
  39.                                     System.out.print("      You Chose Rock    ");   //User's Choice
  40.                                     if(compChoice == 0)
  41.                                     {
  42.                                             System.out.println(" I Chose Rock as well. We tie.");
  43.                                             tie++;
  44.                                     }
  45.                                     if(compChoice == 1)
  46.                                     {
  47.                                             System.out.println(" I Chose Paper. You lose.");
  48.                                             lose++;
  49.                                     }
  50.                                     if(compChoice == 2)
  51.                                     {
  52.                                             System.out.println(" I Chose Scissors. You win!");
  53.                                             win++;
  54.                                     }
  55.      
  56.                             }
  57.                             else if(choiceString.equalsIgnoreCase ("P"))
  58.                             {
  59.                                     System.out.print("      You Chose Paper    ");   //User's Choice
  60.                                     if(compChoice == 0)
  61.                                     {
  62.                                             System.out.println(" I Choose Rock. You win!");
  63.                                             win++;
  64.                                     }
  65.                                     if(compChoice == 1)
  66.                                     {
  67.                                             System.out.println(" I Choose Paper As Well. We tie.");
  68.                                             tie++;
  69.                                     }
  70.                                     if(compChoice == 2)
  71.                                     {
  72.                                             System.out.println(" I Choose: Scissors. You lose.");
  73.                                             lose++;
  74.                                     }
  75.                             }
  76.                             else if(choiceString.equalsIgnoreCase("S"))
  77.                             {
  78.                                     System.out.print("      You Chose Scissors  ");   //User's Choice
  79.                                     if(compChoice == 0)
  80.                                     {
  81.                                             System.out.println(" I Choose: Rock. You lose.");
  82.                                             lose++;
  83.                                     }
  84.                                     if(compChoice == 1)
  85.                                     {
  86.                                             System.out.println(" I Choose: Paper. You win!");
  87.                                             win++;
  88.                                     }
  89.                                     if(compChoice == 2)
  90.                                     {
  91.                                             System.out.println(" I Choose: Scissors as well. We tie.");
  92.                                             tie++;
  93.                                     }
  94.                             }
  95.                             else if(choiceString.equalsIgnoreCase("Q"))
  96.                             {
  97.                                     System.out.println("   Game Over.");
  98.                                     playAgain = "N";                                
  99.                                     break;
  100.                             }
  101.                             else
  102.                             {
  103.                                     System.out.println("    Remember (R) for rock, (P) for paper, or (S) for scissors.  You can type (Q) to exit. ");
  104.                             }
  105.                     }
  106.                     while(true);
  107.            
  108.                     System.out.println("We played " + round + " rounds------- You Won " + win + " times------- You Lost " + lose + " times------- We Tied " + tie + " times-------");
  109.                     percent = (win*100/round);
  110.                     System.out.println(" You win percentage is " + percent + "%");
  111.                    
  112.                     playAgain = JOptionPane.showInputDialog( "Would you like to play RPS?");
  113.    
  114.                    
  115.                 }
  116.         }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement