这个问题在这里已经有答案了:
如何将int转换为string(20个答案)
17天前关门了。
我是建立这个石头剪刀布游戏对电脑,我需要找出如何让用户知道什么是电脑的选择。我用数字来决定胜利者,但现在我需要用一个字符串来让计算机的决定足够清楚。
这是我的密码:
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();
}
}
}
1条答案
按热度按时间bkkx9g8r1#
可以使用string.valueof(int)