{"id":245759,"date":"2025-07-06T12:18:37","date_gmt":"2025-07-06T12:18:37","guid":{"rendered":"https:\/\/learnexams.com\/blog\/?p=245759"},"modified":"2025-07-06T12:18:39","modified_gmt":"2025-07-06T12:18:39","slug":"in-this-problem-youll-play-a-simple-rock-paper-scissors-game","status":"publish","type":"post","link":"https:\/\/www.learnexams.com\/blog\/2025\/07\/06\/in-this-problem-youll-play-a-simple-rock-paper-scissors-game\/","title":{"rendered":"In this problem, you&#8217;ll play a simple rock, paper, scissors game."},"content":{"rendered":"\n<p>In this problem, you&#8217;ll play a simple rock, paper, scissors game. First, you&#8217;ll ask the user to pick rock, paper, or scissors. Then, you&#8217;ll have the computer randomly choose one of the options. You should use the Randomizer class that you created in the last exercise to help the computer make a choice. After that, print out the winner! You should keep playing the game until the user hits enter. Note: You&#8217;ll need to implement a method called String getWinner(String user, String computer) that determines whether the user or computer won the game, and return the correct winner! Here is a sample run of the program. Enter your choice (rock, paper, or scissors): rock User: rock Computer: rock Tie Enter your choice (rock, paper, or scissors): paper User: paper Computer: rock User wins! Enter your choice (rock, paper, or scissors): Thanks for playing! import java.util.Scanner; public class RockPaperScissors { private static final String USER_PLAYER = &#8220;User wins!&#8221;; private static final String COMPUTER_PLAYER = &#8220;Computer wins!&#8221;; private static final String TIE = &#8220;Tie&#8221;; public static String getWinner(String user, String computer) { } public static void main(String[] args) { } } \/\/ Copy over your code from the Randomizer class exercise public class Randomizer { public static int nextInt() { \/\/ Implement this method to return a random number from 1-10 } public static int nextInt(int min, int max) { \/\/ Implement this method to return a random integer between min and max } }<\/p>\n\n\n\n<p><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-ast-global-color-1-color\">The Correct Answer and Explanation is:<\/mark><\/strong><\/p>\n\n\n\n<p>Here&#8217;s the complete implementation of the RockPaperScissors game using the provided structure, along with the Randomizer class:<\/p>\n\n\n\n<p>java<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.Scanner;\n\npublic class RockPaperScissors {\n    private static final String USER_PLAYER = \"User wins!\";\n    private static final String COMPUTER_PLAYER = \"Computer wins!\";\n    private static final String TIE = \"Tie\";\n\n    public static String getWinner(String user, String computer) {\n        if (user.equals(computer)) {\n            return TIE;\n        } else if ((user.equals(\"rock\") &amp;&amp; computer.equals(\"scissors\")) ||\n                   (user.equals(\"paper\") &amp;&amp; computer.equals(\"rock\")) ||\n                   (user.equals(\"scissors\") &amp;&amp; computer.equals(\"paper\"))) {\n            return USER_PLAYER;\n        } else {\n            return COMPUTER_PLAYER;\n        }\n    }\n\n    public static void main(String&#91;] args) {\n        Scanner scanner = new Scanner(System.in);\n        while (true) {\n            System.out.print(\"Enter your choice (rock, paper, or scissors): \");\n            String userChoice = scanner.nextLine().toLowerCase();\n            if (userChoice.equals(\"\")) {\n                System.out.println(\"Thanks for playing!\");\n                break;\n            }\n\n            int computerNum = Randomizer.nextInt(1, 3);\n            String computerChoice = \"\";\n            if (computerNum == 1) {\n                computerChoice = \"rock\";\n            } else if (computerNum == 2) {\n                computerChoice = \"paper\";\n            } else {\n                computerChoice = \"scissors\";\n            }\n\n            System.out.println(\"User: \" + userChoice);\n            System.out.println(\"Computer: \" + computerChoice);\n            System.out.println(getWinner(userChoice, computerChoice));\n        }\n        scanner.close();\n    }\n}\n\nclass Randomizer {\n    public static int nextInt() {\n        return (int)(Math.random() * 10) + 1;\n    }\n\n    public static int nextInt(int min, int max) {\n        return (int)(Math.random() * (max - min + 1)) + min;\n    }\n}\n<\/code><\/pre>\n\n\n\n<p>\ud83e\udde0 <strong>Explanation<\/strong><\/p>\n\n\n\n<p>This program simulates a rock-paper-scissors game between a user and the computer. The game continues in a loop until the user presses enter without typing a choice. The core logic is divided into two classes: <code>RockPaperScissors<\/code> for the game mechanics and <code>Randomizer<\/code> for generating random numbers.<\/p>\n\n\n\n<p>The <code>getWinner<\/code> method determines the outcome of each round. It compares the user&#8217;s and computer&#8217;s choices. If both choices are the same, it returns &#8220;Tie&#8221;. If the user&#8217;s choice beats the computer&#8217;s according to the rules of the game, it returns &#8220;User wins!&#8221;. Otherwise, it returns &#8220;Computer wins!&#8221;. This method uses conditional logic to evaluate all possible outcomes.<\/p>\n\n\n\n<p>In the <code>main<\/code> method, the program prompts the user for input. It reads the user&#8217;s choice and converts it to lowercase to ensure consistency. If the input is empty, the loop breaks and the game ends with a thank-you message.<\/p>\n\n\n\n<p>The computer&#8217;s choice is generated using the <code>Randomizer.nextInt(1, 3)<\/code> method. This produces a random integer between 1 and 3, which is then mapped to &#8220;rock&#8221;, &#8220;paper&#8221;, or &#8220;scissors&#8221;. This ensures that the computer&#8217;s choice is unpredictable and fair.<\/p>\n\n\n\n<p>The <code>Randomizer<\/code> class provides two methods. The first returns a random integer from 1 to 10. The second returns a random integer between any two specified bounds. These methods use <code>Math.random()<\/code> to generate pseudo-random values.<\/p>\n\n\n\n<p>Overall, the program demonstrates basic control structures, user input handling, random number generation, and conditional logic. It is a simple yet effective example of interactive programming in Java.<\/p>\n\n\n\n<p><a href=\"https:\/\/github.com\/ZeroDayTea\/CodeHS-Nitro-Solutions\/tree\/77666b2539323775a487577dd317b9f26348d9ef\/Unit5-WritingClasses%2F5.7.6%2FRockPaperScissors.java\" target=\"_blank\" rel=\"noreferrer noopener\"><\/a><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/learnexams.com\/blog\/wp-content\/uploads\/2025\/07\/learnexams-banner5-710.jpeg\" alt=\"\" class=\"wp-image-245761\"\/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>In this problem, you&#8217;ll play a simple rock, paper, scissors game. First, you&#8217;ll ask the user to pick rock, paper, or scissors. Then, you&#8217;ll have the computer randomly choose one of the options. You should use the Randomizer class that you created in the last exercise to help the computer make a choice. After that, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[25],"tags":[],"class_list":["post-245759","post","type-post","status-publish","format-standard","hentry","category-exams-certification"],"_links":{"self":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/posts\/245759","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/comments?post=245759"}],"version-history":[{"count":0,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/posts\/245759\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/media?parent=245759"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/categories?post=245759"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/tags?post=245759"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}