Advertisement
Guest User

Untitled

a guest
Sep 20th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.Random;
  3.  
  4. /*Program Name: RockPaperScissors.java
  5. Author: Yumi Lamansky
  6. Class: CSC110 CRN 37211
  7. Date Written: 09/18/2018
  8. Brief Description: Simulates rock paper scissors game;
  9. user against the computer
  10. Description of inputs: Rock, Paper, or Scissors
  11. Description of outputs: Users choice, computers choice,
  12. Winner
  13. */
  14.  
  15. public class RockPaperScissors
  16. {
  17. public static void main(String[] args)
  18. {
  19.  
  20. Random random = new Random();
  21. Scanner keyboard = new Scanner(System.in);
  22. String user;
  23. String winner;
  24.  
  25. System.out.println("*****ROCK PAPER SCISSORS GAME*****");
  26.  
  27. //Computers choice
  28. int num;
  29. num = random.nextInt(3) + 1;
  30. String comp = "";
  31.  
  32. if (num == 1)
  33. {
  34. comp = "Rock";
  35. }
  36. else if (num == 2)
  37. {
  38. comp = "Paper";
  39. }
  40. else if (num == 3)
  41. {
  42. comp = "Scissors";
  43. }
  44.  
  45.  
  46. //user picks
  47. System.out.println("Enter one of the following: " +
  48. "\n*Rock\n*Paper\n*Scissors");
  49. user = keyboard.nextLine();
  50.  
  51.  
  52. //invalid input
  53. boolean invalid = false;
  54.  
  55.  
  56. //when winning
  57. String win = "";
  58.  
  59. //ROCK
  60. if (comp.equals("Rock") && user.equalsIgnoreCase("Scissors"))
  61. {
  62. win = "Computer";
  63. }
  64. else if (user.equalsIgnoreCase("Rock") && comp.equals("Scissors"))
  65. {
  66. win = "You!";
  67. }
  68.  
  69.  
  70. //PAPER
  71. else if (comp.equals("Paper") && user.equalsIgnoreCase("Rock"))
  72. {
  73. win = "Computer";
  74. }
  75. else if (user.equalsIgnoreCase("Paper") && comp.equals("Rock"))
  76. {
  77. win = "You!";
  78. }
  79.  
  80. //SCISSORS
  81. else if (comp.equals("Scissors") && user.equalsIgnoreCase("Paper"))
  82. {
  83. win = "Computer";
  84. }
  85. else if (user.equalsIgnoreCase("Scissors") && comp.equals("Paper"))
  86. {
  87. win = "You!";
  88. }
  89.  
  90. //TIE
  91. else if (user.equalsIgnoreCase(comp))
  92. {
  93. win = "Tie";
  94. }
  95. else
  96. {
  97. System.out.println("INVALID INPUT");
  98. invalid = true;
  99. }
  100.  
  101.  
  102.  
  103.  
  104. //Display outputs
  105. if (!invalid)
  106. {
  107. System.out.println("You picked: " + user);
  108. System.out.println("Computer picked: " + comp);
  109. System.out.println("Winner: " + win);
  110. }
  111.  
  112.  
  113.  
  114.  
  115.  
  116. }
  117. }
  118.  
  119.  
  120.  
  121. /*Expected output:
  122. *****ROCK PAPER SCISSORS GAME*****
  123. Enter one of the following:
  124. *Rock
  125. *Paper
  126. *Scissors
  127. [ input ]
  128. You picked: [users input]
  129. Computer picked: [random]
  130. Winner: [winner]
  131. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement