Guest User

Untitled

a guest
Jan 22nd, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1.  
  2. // ****************************************************************
  3. // Rock.java
  4. //
  5. // Play Rock, Paper, Scissors with the user
  6. //
  7. // ****************************************************************
  8. import java.util.Scanner;
  9. import java.util.Random;
  10.  
  11. public class rockpaperscissors
  12. {
  13. public static void main(String[] args)
  14. {
  15. String personPlay; //User's play -- "R", "P", or "S"
  16. String computerPlay; //Computer's play -- "R", "P", or "S"
  17. int computerInt; //Randomly generated number used to determine //computer's play
  18.  
  19. Scanner scan = new Scanner(System.in);
  20. Random generator = new Random();
  21.  
  22. //where a random number gets generated
  23. computerInt=generator.nextInt(3);
  24. System.out.println(""+computerInt);
  25.  
  26. //Get player's play -- note that this is stored as a string
  27. System.out.println(("Enter R P S "));
  28. personPlay=scan.next();
  29.  
  30. //Make player's play uppercase for ease of comparison //Generate computer's play (0,1,2)
  31. personPlay= personPlay.toUpperCase();
  32. System.out.println("You chose: " +personPlay);
  33.  
  34. //Translate computer's randomly generated play to string
  35. switch (computerInt){
  36. case 0:computerPlay = "R"; break;
  37. case 1:computerPlay = "P"; break;
  38. case 2:computerPlay = "S"; break;
  39. default:computerPlay= " "; break;
  40. }
  41. //Print computer's play
  42. System.out.println("The computer chose: "+computerPlay);
  43.  
  44. {
  45.  
  46. //See who won. Use nested ifs instead of &&
  47. if (personPlay.equals(computerPlay)){
  48. System.out.println("It's a tie!");
  49. }
  50. if (personPlay.equals("R")){
  51. if (computerPlay.equals("S")){
  52. System.out.println("Rock crushes scissors. You win!!");}
  53. else { System.out.println("Rock crushes scissors. You loose!!");}}
  54.  
  55. if (personPlay.equals("P")){
  56. if (computerPlay.equals("R")){
  57. System.out.println("Paper covers rock. You win!!");}
  58. else {
  59. System.out.println("Paper covers rock. You loose!!");}}
  60.  
  61. if (personPlay.equals("S")){
  62. if (computerPlay.equals("P")){
  63. System.out.println("Scissors cuts paper. You win!!");}
  64. else{ System.out.println("Scissors cuts paper You loose!!");}}
  65. }
  66. }
  67. }
Add Comment
Please, Sign In to add comment