Java用循环语句实现石头剪刀布
时间: 2024-02-13 19:02:58 浏览: 87
好的,以下是Java实现石头剪刀布的代码:
```java
import java.util.Scanner;
import java.util.Random;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Random random = new Random();
String[] options = {"石头", "剪刀", "布"};
while (true) {
System.out.println("请输入你的选项:1.石头 2.剪刀 3.布");
int userOption = input.nextInt();
int computerOption = random.nextInt(3) + 1;
System.out.println("你出了:" + options[userOption - 1]);
System.out.println("电脑出了:" + options[computerOption - 1]);
if (userOption == computerOption) {
System.out.println("平局!");
} else if ((userOption == 1 && computerOption == 2) ||
(userOption == 2 && computerOption == 3) ||
(userOption == 3 && computerOption == 1)) {
System.out.println("你赢了!");
} else {
System.out.println("你输了!");
}
System.out.println("是否继续?1.继续 2.退出");
int continueOption = input.nextInt();
if (continueOption != 1) {
break;
}
}
}
}
```
运行程序后,输入对应的数字即可进行石头剪刀布游戏,输入2退出游戏。
阅读全文