写一段石头剪刀布的Java代码
时间: 2023-02-22 11:21:45 浏览: 239
好的,这是一段简单的石头剪刀布Java代码:
```
import java.util.Scanner;
public class RPS {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请出拳(1. 石头 2. 剪刀 3. 布):");
int playerGesture = sc.nextInt();
int computerGesture = (int) (Math.random() * 3 + 1);
System.out.println("电脑出了: " + computerGesture);
if (playerGesture == computerGesture) {
System.out.println("平局");
} else if ((playerGesture == 1 && computerGesture == 2) ||
(playerGesture == 2 && computerGesture == 3) ||
(playerGesture == 3 && computerGesture == 1)) {
System.out.println("你赢了");
} else {
System.out.println("你输了");
}
}
}
```
代码的思路很简单:读入玩家出的拳,用随机数模拟电脑出的拳,再根据规则判断并输出结果。
阅读全文