我正在构建这个针对计算机的石头剪刀布游戏,我需要弄清楚如何让用户知道计算机的选择是什么。我使用数字来确定获胜者,但现在我需要使用字符串来实际使计算机的决定足够清晰。
这是我的代码:
import java.util.Scanner;
import java.util.Random;
public class RockPaperScissors{
public static void main(String [] args){
Scanner scan = new Scanner(System.in);
Random rand = new Random();
String str = "p";
int scissors = 0;
int rock = 1;
int paper = 2;
int computerChoice = rand.nextInt((paper - scissors) + 1) + scissors;
int userChoice;
int tieCounter = 0;
int winCounter = 0;
int lossCounter = 0;
System.out.println("Let's play a small game of " + "Rock-Paper-Scissors, shall we? ");
System.out.println();
while(str.equalsIgnoreCase("p")){
System.out.println("pick your choice (0 - scissors , 1 - rock , 2 - paper): ");
userChoice = scan.nextInt();
if(userChoice == computerChoice){
System.out.println("that a tie! let's go again!");
tieCounter++;
}
if((userChoice != scissors && userChoice > computerChoice && computerChoice != scissors) || (userChoice != rock && userChoice < computerChoice && computerChoice != rock) || (userChoice != paper && userChoice > computerChoice && computerChoice != paper )){
System.out.println("You won!");
System.out.println();
System.out.println("I chose: " + computerChoice + "," + " you chose: " + userChoice);
System.out.println();
winCounter++;
}
if((computerChoice != scissors && computerChoice > userChoice && userChoice != scissors) || (computerChoice != rock && computerChoice < userChoice && userChoice != rock) || (computerChoice != paper && computerChoice > userChoice && userChoice != paper)){
System.out.println("You lost!");
System.out.println();
System.out.println("I chose: " + computerChoice + "," + " you chose: " + userChoice);
System.out.println();
lossCounter++;
}
System.out.println("You won: " + winCounter + " games, " + "lost: " + lossCounter + " games, " + "tied: " + tieCounter + " games.");
System.out.println();
System.out.println();
System.out.println("Do you want to continue playing? (p/q) : ");
scan.nextLine();
str = scan.nextLine();
}
}
}