所有的语言实现起来都是类似的
import java.util.Scanner;
public class caiquan {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String[] r = {"石头", "剪刀", "布"};
int computerChoice = (int) (Math.random() * 3); // 随机生成计算机出拳
System.out.println("石头剪刀布游戏开始!");
System.out.println("请出拳:0 - 石头,1 - 剪刀,2 - 布");
int userChoice = scanner.nextInt();
if (userChoice < 0 || userChoice > 2) {
System.out.println("无效的输入,请输入0、1或2。");
return;
}
System.out.println("你出了:" + r[userChoice]);
System.out.println("我出了:" + r[computerChoice]);
if (userChoice == computerChoice) {
System.out.println("平局!");
} else if (userChoice == 0 && computerChoice == 1
|| userChoice == 1 && computerChoice == 2
|| userChoice == 2 && computerChoice == 0) {
System.out.println("你赢了!");
} else {
System.out.println("你输了!");
}
}
}