我正在开发一个 Java 程序,我正在努力为我的程序创建我称之为统计区域的东西。我创建了一个“摇滚”“纸”“剪刀”程序,我试图告诉用户他们赢了多少次,计算机赢了多少次,最后,有多少平局。
请原谅我的无知,我正在学习。这是我的代码:
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++);
}
}
}
}
}
基本上我已经做了一个 if 语句,说当用户至少赢一次时,显示统计信息但是这似乎不起作用。我不能 100% 确定如何显示统计信息。