Question
How do I write a flowchart for this program? import java.util.*; import java.util.Random; public class RockPaperScissorGame{ public static String generateComputerChoice(Random random){ //used to assign a
How do I write a flowchart for this program?
import java.util.*; import java.util.Random;
public class RockPaperScissorGame{
public static String generateComputerChoice(Random random){ //used to assign a random number for the string computerChoice int wordNumber; wordNumber = random.nextInt(3) + 1; //shifting the range of random numbers from 0 - 2 to 1 - 3 String computerChoice = ""; if(wordNumber == 1){ //if statement to show the computer what it has chosen based on its random integer computerChoice = "rock"; } else if(wordNumber == 2){ computerChoice = "paper"; } else if(wordNumber == 3){ computerChoice = "scissors"; } System.out.println("The computer has made its choice: "); return computerChoice; //returns the assigned number to the String } public static void showMenu(){ //stored the println statement in showMenu System.out.println("Options : 1. rock 2. paper 3. scissor "); } public static String getUserChoice(Scanner scanner){ //gets the user's input and stores it in the String userChoice String userChoice; System.out.println("User Choice: "); userChoice = scanner.nextLine(); return userChoice; //returns the users input to getUserChoice } public static String chooseWinner(String computerChoice, String userChoice){ //this method takes two arguements and chooses the winner by running through the if statements String winner = "No winner"; //this value is set when none of the if statements are true String customMessage = ""; //set customMessage to whichever prompt is chosen from the if statements String finalMessage; //initializing variable finalMessage String rockCustomMessage = "Rock smashes the scissors"; String scissorsCustomMessage = "Scissors cuts paper"; String paperCustomMessage = "Paper wraps rock"; if(computerChoice.equals("rock") && userChoice.equalsIgnoreCase("scissors")){ //if statement that shows what happens when the computer chooses rock and the user chooses scissors winner = "Computer"; customMessage = rockCustomMessage; } else if(userChoice.equalsIgnoreCase("rock") && computerChoice.equals("scissors")){ //else if statement that shows if the user chooses rock and the computer chooses scissors winner = "You"; customMessage = rockCustomMessage; } if(computerChoice.equals("scissors") && userChoice.equalsIgnoreCase("paper")){ //if statement that shows what happens when the computer chooses scissors and the user chooses paper winner = "Computer"; customMessage = scissorsCustomMessage; } else if(userChoice.equalsIgnoreCase("scissors") && computerChoice.equals("paper")){ //else if statement that shows if the user chooses scissors and the computer chooses paper winner = "You"; customMessage = scissorsCustomMessage; } if(computerChoice.equals("paper") && userChoice.equalsIgnoreCase("rock")){ //if statement that shows what happens when the computer chooses paper and the user chooses rock winner = "Computer"; customMessage = paperCustomMessage; } else if(userChoice.equalsIgnoreCase("paper") && computerChoice.equals("rock")){ //else if statement that shows if the user chooses paper and the computer chooses rock winner = "You"; customMessage = paperCustomMessage; } finalMessage = winner + " won(" + customMessage + ")"; if(finalMessage.equals("No winner won()")){ //if statement that gets rid of the parenthesis if the game is tied finalMessage = "No winner won"; return finalMessage; } else{ return finalMessage; //returns the message to chooseWinner }
} public static void main(String [] args){ //main method Random random = new Random(); //creating random scanner object Scanner scanner = new Scanner(System.in); //creating scanner object String computerChoice; //storing the computers choice in computerChoice String userChoice; //storing the users input in this variable String winner; showMenu(); //shows the user the the print statement computerChoice = generateComputerChoice(random); //gets whatever was returned to generateComputerChoice as the computerChoice userChoice = getUserChoice(scanner); //gets whatever was returned to getUserChoice and stores it in userChoice winner = chooseWinner(computerChoice, userChoice); System.out.println("You chose " + userChoice + " Computer chose " + computerChoice); System.out.println(winner); //prints out the winner (you or the computer) while(winner.equals("No winner won")){ //while loop that starts the game again if the user and computer choose the same thing System.out.println("Same choice, Play again."); showMenu(); //shows the menu again witht the options computerChoice = generateComputerChoice(random); //gets input from computer and stores it userChoice = getUserChoice(scanner); //gets input from user and stores it winner = chooseWinner(computerChoice, userChoice); System.out.println("You chose " + userChoice + " Computer chose " + computerChoice); System.out.println(winner); } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started