ここでコードをコンパイルしてください。私が間違っていることを見つけ出すことはできませんが、基本的には、ユーザーが選択したロック、ペーパー、はさみを取り入れたいと思っています。プログラムをランダムに選択して、プログラムに勝者が誰であるかを表示したい。JavaでこのRockScissorPaperゲームを入手して、ユーザーまたはコンピュータが勝ったかどうかを教えてもらう方法がわからない
import java.util.Random;
import java.util.Scanner;
public class RockPaperScissor {
private User user;
private Computer computer;
private int userScore;
private int computerScore;
private int numberOfGames;
private enum Move {
ROCK, PAPER, SCISSORS;
public int compareMoves(Move otherMove) {
// A tie
if (this == otherMove)
return 0;
switch (this) {
case ROCK:
return (otherMove == SCISSORS ? 1 : -1);
case PAPER:
return (otherMove == ROCK ? 1 : -1);
case SCISSORS:
return (otherMove == PAPER ? 1 : -1);
}
return 0;
}
}
private class User {
private Scanner inputScanner;
public User(){
inputScanner = new Scanner(System.in);
}
public Move getMove(){
System.out.println("Make you choice: Rock, paper or scissors");
String userInput = inputScanner.nextLine();
userInput = userInput.toUpperCase();
char firstletter = userInput.charAt(0);
char firstLetter = 0;
if (firstLetter == 'R' || firstLetter == 'P' || firstLetter == 'S') {
switch (firstLetter) {
case 'R':
return Move.ROCK;
case 'P':
return Move.PAPER;
case 'S':
return Move.SCISSORS;
}
}
return getMove();
}
public boolean playAgain(){
System.out.print("Do you want to play again?");
String userInput = inputScanner.nextLine();
userInput = userInput.toUpperCase();
return userInput.charAt(0) == 'Y';
}
}
private class Computer {
public Move getMove() {
Move[] moves = Move.values();
Random random = new Random();
int index = random.nextInt(moves.length);
return moves[index];
}
}
public RockPaperScissor() {
user = new User();
computer = new Computer();
userScore = 0;
computerScore = 0;
numberOfGames = 0;
}
public void startGame() {
System.out.println("ROCK, PAPER, SCISSORS!");
// Get moves
Move userMove = user.getMove();
Move computerMove = computer.getMove();
System.out.println("\nYou played " + userMove + ".");
System.out.println("Computer played " + computerMove + ".\n");
// Compare moves and determine winner
int compareMoves = userMove.compareMoves(computerMove);
switch (compareMoves) {
case 0: // Tie
System.out.println("Tie!");
break;
case 1: // User wins
System.out.println(userMove + " beats " + computerMove + ". You won!");
userScore++;
break;
case -1: // Computer wins
System.out.println(computerMove + " beats " + userMove + ". You lost.");
computerScore++;
break;
}
numberOfGames++;
// Ask the user to play again
if (user.playAgain()) {
System.out.println();
startGame();
} else {
printGameStats();}
}
private void printGameStats() {
int wins = userScore;
int losses = computerScore;
int ties <= numberOfGames - userScore = computerScore;
double percentageWon = (wins + ((double) ties)/2)/numberOfGames;
System.out.print("+");
printDashes(68);
System.out.println("+");
System.out.printf("| %6s | %6s | %6s | %12s | %14s | \n",
"WINS", "LOSSES", "TIES", "GAMES PLAYED", "PERCENTAGE WON");
System.out.print("|");
printDashes(10);
System.out.println("+");
printDashes(10);
System.out.print("+");
printDashes(10);
System.out.print("+");
printDashes(16);
System.out.print("+");
printDashes(18);
System.out.println("|");
System.out.printf("| %6d | %6d | %6d | %%12d | %13.2f%% |\n",
wins, losses, ties, numberOfGames, percentageWon * 100);
System.out.print("+");
printDashes(68);
System.out.println("+");
}
private void printDashes(int numberOfDashes) {
for (int i =0; i < numberOfDashes; i++) {
System.out.print("-");
}
}
public static void main(String[] args) {
RockPaperScissor game = new RockPaperScissor();
game.startGame();
}
}
あなたはどんな問題がありますか? 'Computer.getMove()'をもっとコンパクトにすることができる以外は 'return Move.values()[new Random()。nextInt(Move.values()。length)];'? – KevinO
さて、私がプログラムを実行すると、私は何を選択しているのかを尋ね続け、コンピュータが決定したものと誰が勝ったのかを表示しません。 –