- 分享
- 0
- 人气
- 0
- 主题
- 33
- 帖子
- 84
- UID
- 376337
- 积分
- 964
- 阅读权限
- 17
- 注册时间
- 2010-12-8
- 最后登录
- 2019-3-25
- 在线时间
- 132 小时
|
:Create a game in which let the user enter rock, paper or scissors, and the computer randomly chooses rock, paper or scissors. Then determine the winner.
Your games shall :
•Make sure the game works correctly whether the player enter a choice in uppercase or lowercase letters or a combination of the two
•To allow for player misspellings, accept the player’s entry as long as the first two letters are correct. (In other words, if a player types “scixxrs”, you will accept it as “scissors”, because at least the first two letter are correct.
•When the player does not type at least the first two letters of the choice correctly, reprompt the player and continue to do so until the player’s entry contains at least the first two letters of one of the options
•Allow 10 complete rounds of the games. At the end, display counts of the number of times the player won, the number of times the computer won, and the number of tie games.
[Hints : Math.random() return a value of type double equal to 0.0 or less than 1.0 . To find absolute value of num-guess, use the expression Math.abs(num-guess)]
不知道我做对吗,请大家改正我,如不对的话。
package javaapplication4;
import java.io.*;
public class JavaApplication4 {
public static void main(String[] args) {
int userScore = 0;
int compScore = 0;
String userMove, compMove;
int winner;
System.out.println("ROCK - PAPER - SCISSORS");
System.out.println("This game plays 10 rounds.");
for (int rnd=1; rnd<=10; rnd++) {
userMove = getUserMove();
compMove = getComputerMove();
System.out.println("Computer's move: " + compMove);
winner = determineRoundWinner(compMove, userMove);
if (winner == 1) {
System.out.println(compMove + " beats " + userMove + " the computer wins this round.");
compScore++;
}
else if (winner == -1) {
System.out.println(userMove + " beats " + compMove + " the user wins this round.");
userScore++;
}
else {
System.out.println(userMove + " ties " + compMove + " nobody wins this round.");
}
System.out.println("Score: User=" + userScore + " Computer=" + compScore);
System.out.println();
}
displayGameWinner(userScore, compScore);
}
static String getUserMove() {
String userMove = "club";
while (!userMove.equals("ROCK") && !userMove.equals("PAPER") &&!userMove.equals("SCISSORS")) {
System.out.print("Enter your move " + "[ROCK, PAPER, SCISSORS]: ");
userMove = Console.in.readLine();
userMove = userMove.toUpperCase();
}
return userMove;
}
static String getComputerMove() {
int compMoveInt;
String compMove;
compMoveInt = randomInt(1,3);
if (compMoveInt == 1) {
compMove = "ROCK";
}
else if (compMoveInt == 2) {
compMove = "PAPER";
}
else {
compMove = "SCISSORS";
}
return compMove;
}
public static int randomInt(int lowEnd, int highEnd) {
int theNum;
theNum = (int)(Math.random() * (highEnd - lowEnd + 1)) + lowEnd;
return theNum;
}
static int determineRoundWinner(String userMove,
String compMove) {
int winner;
if (compMove.equals(userMove)) {
winner = 0;
}
else if (compMove.equals("ROCK") && userMove.equals("SCISSORS")) {
winner = 1;
}
else if (compMove.equals("PAPER") &&
userMove.equals("ROCK")) {
winner = 1;
}
else if (compMove.equals("SCISSORS") &&
userMove.equals("PAPER")) {
winner = 1;
}
else {
winner = -1;
}
return winner;
}
static void displayGameWinner(int userScore,
int compScore) {
System.out.println("\n\nFinal Score:");
System.out.println(" User=" + userScore + " Computer=" + compScore);
System.out.println();
if (userScore > compScore) {
System.out.println("The user wins!");}
else if (compScore > userScore) {
System.out.println("The computer wins!");
}
else {
System.out.println("Its a tie, nobody wins.");
}
}
} |
|