石头剪刀布java简单代码
时间: 2023-11-29 07:46:21 浏览: 279
以下是一个简单的Java代码实现石头剪刀布游戏的例子:
```java
import java.util.Scanner;
import java.util.Random;
public class RockPaperScissors {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Random random = new Random();
String[] options = {"石头", "剪刀", "布"};
int computerOption = random.nextInt(3); System.out.print("请输入你的选择(0-石头,1-剪刀,2-布):");
int userOption = input.nextInt();
System.out.println("你的选择是:" + options[userOption]);
System.out.println("计算机的选择是:" + options[computerOption]);
if (userOption == computerOption) {
System.out.println("平局!");
} else if ((userOption == 0 && computerOption == 1) || (userOption == 1 && computerOption == 2) || (userOption == 2 && computerOption == 0)) {
System.out.println("你赢了!");
} else {
System.out.println("你输了!");
}
}
}
```
阅读全文