Java代写: CS 1054 Introduction to Programming in Java代写

Java代写一个剪刀石头布的程序。用户输入自己选择的Rock, Paper, Scissors,和系统随机产生的Rock, Paper, Scissors进行比较,在console中输出胜负。

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

(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

(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.
kamisama wechat