java猜拳游戏代码
时间: 2024-01-02 08:19:15 浏览: 125
好的,以下是一个简单的 Java 猜拳游戏代码,有石头、剪刀、布三种选项,可以让玩家和电脑进行对战:
```java
import java.util.Scanner;
import java.util.Random;
public class Guess {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Random rand = new Random();
String[] options = {"石头", "剪刀", "布"};
int winCount = 0, loseCount = 0, drawCount = 0;
System.out.println("欢迎来到猜拳游戏!请输入您的名字:");
String name = input.nextLine();
System.out.println("您好," + name + "!请选择您要出的拳头(1:石头;2:剪刀;3:布):");
while (true) {
int player = input.nextInt();
if (player < 1 || player > 3) {
System.out.println("输入有误,重新输入!");
continue;
}
int computer = rand.nextInt(3) + 1;
System.out.println(name + "出了" + options[player - 1] + ",电脑出了" + options[computer - 1]);
if (player == computer) {
System.out.println("平局!");
drawCount++;
} else if ((player == 1 && computer == 2) || (player == 2 && computer == 3) || (player == 3 && computer == 1)) {
System.out.println("您赢了!");
winCount++;
} else {
System.out.println("电脑赢了!");
loseCount++;
}
System.out.println("您已经赢了" + winCount + "局,输了" + loseCount + "局,平了" + drawCount + "局。是否继续游戏?(Y/N)");
if (input.next().equalsIgnoreCase("N")) {
break;
}
}
System.out.println("游戏结束,谢谢您的参与!");
}
}
```
阅读全文