私は Java プログラムに取り組んでおり、自分のプログラムの統計領域と呼んでいるものを作成するのに苦労しています。「Rock」「Paper」「Scissors」プログラムを作成しました。ユーザーに、ユーザーが何回勝ったか、コンピューターが何回勝ったか、最後に何回引き分けたかを伝えようとしています。
私の無知を許してください、私は学んでいます。これが私のコードです:
import java.util.Scanner;
public class TheAntlers {
public static void main(String[] args) {
int playerHumanWins = 0;
int playerComputerWins = 0;
int numberOfTies = 0;
int computerResult;
Scanner input = new Scanner(System.in);
while(true) {
String startGame;
String playerHuman;
String playerComputer = " ";
System.out.print("Do you want to play \"Rock\", \"Paper\", \"Scissors\"? (Y/N): ");
startGame = input.nextLine().toUpperCase();
if(startGame.equals("N")) {
System.out.println("NO!");
break;
}
else if(! startGame.equals("Y")) {
startGame = startGame.toLowerCase();
System.out.println("Sorry, " + startGame + " is not a valid entry...");
}
while(startGame.equals("Y")) {
System.out.print("Please choose \"Rock\", \"Paper\", or \"Scissors\": ");
playerHuman = input.nextLine().toUpperCase();
computerResult = (int)(Math.random() * 3);
if(computerResult == 0) {
playerComputer = "ROCK";
}
else if(computerResult == 1) {
playerComputer = "PAPER";
}
else if (computerResult == 2) {
playerComputer = "SCISSORS";
}
switch (playerHuman) {
case "ROCK" :
if(playerComputer.equals(playerHuman)) {
System.out.println("Tie you both picked \"ROCK\"");
numberOfTies++;
}
else if(playerComputer.equals("PAPER")) {
System.out.println("Sorry, the computer wins!");
playerComputerWins++;
}
else {
System.out.println("You win, \"ROCK\" beats " + "\"" + playerComputer + "\"");
playerHumanWins++;
return;
}
break;
case "PAPER" :
if(playerComputer.equals(playerHuman)) {
System.out.println("Tie you both picked \"PAPER\"");
numberOfTies++;
}
else if(playerComputer.equals("ROCK")) {
System.out.println("You win, \"PAPER\" beats " + "\"" + playerComputer + "\"");
playerHumanWins++;
return;
}
else {
System.out.println("Sorry, the computer won!");
playerComputerWins++;
}
break;
case "SCISSORS" :
if(playerComputer.equals(playerHuman)) {
System.out.println("Tie you both picked \"SCISSORS\"");
numberOfTies++;
}
else if(playerComputer.equals("PAPER")) {
System.out.println("You win, \"SCISSORS\" beats " + "\"" + playerComputer + "\"");
playerHumanWins++;
return;
}
else {
System.out.println("Sorry, the computer won!");
playerComputerWins++;
}
break;
default:
playerHuman = playerHuman.toLowerCase();
System.out.println("Sorry, " + playerHuman + " is not a valid entry...");
}
if(playerHumanWins == 1) {
System.out.println("You won: " + playerHumanWins + " times");
System.out.println("The computer won " + playerComputerWins + " times");
System.out.println("There were " + numberOfTies++);
}
}
}
}
}
基本的に、ユーザーが少なくとも1回勝ったときに統計を表示するというifステートメントを作成しましたが、これは機能していないようです。統計を表示する方法が 100% わかりません。