The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Apr 2017 |
| Last Sign in: | 103 Weeks Ago, 2 Days Ago |
| Questions Answered: | 4870 |
| Tutorials Posted: | 4863 |
MBA IT, Mater in Science and Technology
Devry
Jul-1996 - Jul-2000
Professor
Devry University
Mar-2010 - Oct-2016
Can someone help me with my project of Java ASAP please? My code run perfectly fine but it does not fit the teacher's requirement. So I desperately need someone to help to fix it. I attached my code and project requirement and please read carefully about the project description. Thank you so much and please let me know if you have additional questions!Â
Â
Â
import java.util.Scanner;
import java.util.Random;
public class RockPaperScissors
{
private static int versus(int in)
{
int v;
Random rgen = new Random();
int compare = rgen.nextInt(3);
if(compare == 0)
{
if(compare == in)
{
System.out.println("Tie!");
v = 0;
}
else if(in == 1)
{
System.out.println("Your Point!");
System.out.println("P beats R");
v=1;
}
else
{
System.out.println("My point!");
System.out.println("R beats S");
v = 2;
}
}
else if(compare == 1)
{
if(compare == in)
{
System.out.println("Tie!");
v=0;
}
else if(in == 0)
{
System.out.println("My Point!");
System.out.println("P beats R");
v = 2;
} else if(in == 2)
{
System.out.println("Your Point!");
System.out.println("S beats P");
v = 1;
}
} } else
{
if(compare == in)
{
System.out.println("Tie!");
v = 0;
}
else if(in == 0)
{
System.out.println("My Point!");
System.out.println("R beats S");
v = 2;
}
else if(in == 1)
{
System.out.println("Your Point!");
System.out.println("S beats P");
v = 1;
}
}
return v; private static int exchange(String ins)
{
int rep;
if(ins.equals("R"))
{
rep = 0;
}
else if(ins.equals("P"))
{
rep = 1;
}
else if(ins.equals("S"))
{ rep = 2; }
else if(ins.equals("quit"))
{
rep = -2;
}
else
{
rep = -1;
System.out.println("Invalid input.");
}
return(rep);
}
public static void main(String args)
{
Scanner keyboard = new Scanner(System.in);
int inx = 0;
int win = 0;
int tie = 0;
int lost = 0;
int n = 0;
String input = "";
while(true)
{
System.out.print("(R)ock, (P)aper, (S)cissors, or quit: ");
input = keyboard.nextLine();
int users = exchange(input);
if(users == -2)
{
System.out.println("");
break; }
else if(users != -1)
{
n = versus(users);
if(n == 0)
tie++;
else if(n == 1)
win++;
else if(n == 2)
lost++;
} }
System.out.println("You won " + win + " times.");
System.out.println("You lost " + lost + " times.");
System.out.println("We tied " + tie + " times.");
} }
Rock - Paper - Scissors
Program Behavior
Your program will allow the user to play the Rock-Paper-Scissors game against the
computer. In case you're not familiar with the game: when two people play, they both
choose one of three options (Rock, Paper, or Scissors) at the same time, with the
appropriate hand gesture, and see who wins. Rock beats Scissors (because rock breaks scissors) Paper beats Rock (because paper covers rock) Scissors beats Paper (because scissors cut paper) You can also tie by choosing the same option.
Your program will repeatedly prompt the user for his or her choice, then announce who
won. Keep prompting for and processing input until the user enters the word "quit" (this
is the sentinel value used to conclude the input). After the user quits, you will print how
a summary of the results.
EDIT: After checking for the sentinel value, verify that the user input is valid. If the user
enters anything anything other than "R", "P", or "S", simply print "Invalid input.". Invalid
input does not affect the results summary.
Here is a sample run of the program. User input is shown in red. Make sure your
prompts and the output match this text and format:
(R)ock, (P)aper, (S)cissors, or quit: R
Tie! (R)ock, (P)aper, (S)cissors, or quit: R
Your Point!
R beats S (R)ock, (P)aper, (S)cissors, or quit: S
Your Point!
S beats P (R)ock, (P)aper, (S)cissors, or quit: P
My Point! S beats P (R)ock, (P)aper, (S)cissors, or quit: X
Invalid input. (R)ock, (P)aper, (S)cissors, or quit: S
Tie! (R)ock, (P)aper, (S)cissors, or quit: R
Your Point!
R beats S (R)ock, (P)aper, (S)cissors, or quit: quit You won 3 times.
You lost 1 times.
We tied 2 times. Set Up and Design
Create a new BlueJ project called Project1 and inside that create a class
called RockPaperScissors. This will be the only class you'll need to write for this
program.
Add a main method to that class. You will run the main method to run the
program. The main method will make use of two additional methods that you will
include in the RockPaperScissors class: The getComputerChoice method will pick a random choice of the three options
for the computer. The method takes no parameters and returns a String that is
either "R", "P", or "S". The playerWins method will determine if the player wins the current round. The
method takes two String parameters, the first representing the player's choice and
the second the computer's choice. It should return a boolean value: true if the
player wins and false otherwise. All other work for the program will be done in the main method. Since the support
methods will be called from the main method, they should both be declared as static. Remember to include JavaDoc comments for the class and each method. Include all
appropriate tags. Update the comments as needed as you refine your program. Developing the Program
As with almost all programs: work on it in stages. Don't try to get the whole thing
written before compiling and testing it. For example, you could: Get the main method set up, then add a while loop that does nothing but read
user input until the user enters "quit". Prompt for and read the first choice before the
loop, then prompt and read again inside the loop at the bottom. Compile and test
that small program. Then you can move forward with confidence knowing that that
part of the program is working. Now write the getComputerChoice method. Call it from the main method in
the loop to pick the computer's choice each time. For now just print out the choice.
Test that. Then check to see if the computer's choice is the same as the player's choice. If
so, print the proper "Tie!" output. Test. Declare integers to let you track the number of ties, user wins, and computer
wins. Increment the tie count where appropriate. After the loop, print the results.
Test. Etc. Whenever you are comparing strings in this program, use the equals method
of the String class, not the == operator.
Remember, there is no single, magic way a program can be written. You, the
programmer, make the program do what you want it to do.
On the other hand, a working program is not necessarily a good program. Always
look for opportunities to improve your code. Have you picked meaningful variable
names? Is there unnecessary logic that can be eliminated? Are you doing things inside
loops that you don't have to do each time? Are your comments accurate and helpful?
-----------