java:石头剪刀布
时间: 2024-01-12 20:22:07 浏览: 158
以下是使用Java程序代码完成石头剪刀布游戏的示例:
```java
import java.util.Scanner;
import java.util.Random;
public class RockPaperScissors {
public static void main(String[] args) {
String[] choices = {"石头", "剪刀", "布"};
Scanner scanner = new Scanner(System.in);
Random random = new Random();
System.out.println("欢迎来到石头剪刀布游戏!请选择:");
System.out.println("1. 石头");
System.out.println("2. 剪刀");
System.out.println("3. 布");
int playerChoice = scanner.nextInt();
int computerChoice = random.nextInt(3) + 1;
System.out.println("你选择了:" + choices[playerChoice - 1]);
System.out.println("电脑选择了:" + choices[computerChoice - 1]);
if (playerChoice == computerChoice) {
System.out.println("平局!");
} else if ((playerChoice == 1 && computerChoice == 2) ||
(playerChoice == 2 && computerChoice == 3) ||
(playerChoice == 3 && computerChoice == 1)) {
System.out.println("你赢了!");
} else {
System.out.println("你输了!");
}
}
}
```
阅读全文