Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I did the code for this task and got 75/100 with opportunity to fix: This i the question and the correction the profesor is asking

I did the code for this task and got 75/100 with opportunity to fix:

This i the question and the correction the profesor is asking for is : Use switches for part 5

Part 5

For the final part of the lab, we will ask you to create a version of rock-paper-scissors game. This program will take a user input of 1, 2, or 3 then generate a random number and print the winner.

Create a method called intToWord which takes as an argument an integer. Using a switch, return "rock" if the argument is 0, "paper" for 1, and "scissors" for 2. If the integer is not 0, 1, or 2 return "invalid selection".

Create a method score which takes as arguments two integers: userChoice and compChoice, and determines the winner. It should return one of three strings:

"Tie." " beats . I win." " beats . You win."

Create a method called chooseString, which takes a String person and an integer choice, then prints:

" chose ." eg. "You chose rock."

Fill out the main method such that a user must choose a number, then the computer randomly generates a number between 0 and 2, and then one of the the following strings are printed by calling the methods you wrote previously:

"You chose . I chose .  beats .  win." or "You chose . I chose . Tie." e.g. "You chose rock. I chose paper. paper beats rock. I win." e.g. "You chose paper. I chose paper. Tie." e.g. "You chose paper. I chose rock. paper beats rock. You win."

Extra (Not for credit): Can you adjust your program so that ties cause you to re-run the game?

that's my code:

import java.io.*; import java.util.*; class Main { public static String intToWord(int a) { if (a == 0) return "rock"; if (a == 1) return "paper"; return "scissors"; } public static String findWinner(int a, int b) { if (a == b) { return "Tie."; } String ch1 = intToWord(a); String ch2 = intToWord(b); if (a == 0) { if (b == 1) { return ch2 + " beats " + ch1 + ". You Win."; } if (b == 2) { return ch1 + " beats " + ch2 + ". I Win."; } } if (a == 1) { if (b == 0) { return ch1 + " beats " + ch2 + ". I Win."; } if (b == 2) { return ch2 + " beats " + ch1 + ". You Win."; } } if (a == 2) { if (b == 0) { return ch2 + " beats " + ch1 + ". You Win."; } if (b == 1) { return ch1 + " beats " + ch2 + ". I Win."; } } return ch1 + " beats " + ch2 + ". I Win."; } public static void main(String[] args) throws IOException { Scanner obj = new Scanner(System.in); while(true) { System.out.println("Enter your choice"); int user = obj.nextInt(); Random r = new Random(); int computer = r.nextInt(3); System.out.println("You choose " + intToWord(computer) + ". I choose " + intToWord(user) + ". " + findWinner(user, computer)); if(user!=computer){ break; } } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Spatial Databases A Tour

Authors: Shashi Shekhar, Sanjay Chawla

1st Edition

0130174807, 978-0130174802

More Books

Students also viewed these Databases questions