用java编写剪刀石头布
时间: 2023-09-30 11:06:05 浏览: 76
好的,我可以帮您用 Java 编写一个简单的剪刀石头布游戏。可以参考下面的代码实现:
```
import java.util.Random;
import java.util.Scanner;
public class RockPaperScissors {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Random random = new Random();
System.out.println("欢迎来到剪刀石头布游戏!");
while (true) {
// 读取玩家输入
System.out.print("请输入您的选择(0-剪刀,1-石头,2-布):");
int player = scanner.nextInt();
// 随机生成电脑的选择
int computer = random.nextInt(3);
// 判断胜负
if (player == computer) {
System.out.println("平局!");
} else if ((player == 0 && computer == 1) || (player == 1 && computer == 2) || (player == 2 && computer == 0)) {
System.out.println("您输了!");
} else {
System.out.println("您赢了!");
}
// 询问是否继续
System.out.print("是否继续(y/n)?");
String answer = scanner.next();
if (answer.equalsIgnoreCase("n")) {
break;
}
}
System.out.println("游戏结束,谢谢参与!");
}
}
```
该程序实现了剪刀石头布游戏的基本逻辑,也支持多次游戏。希望能对您有所帮助!
阅读全文