import java.util.Scanner;
import java.util.Random;
public class Hello {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in); // 创建Scanner对象
Random random = new Random();
int play1, play2;
String[] arr = { "石头", "布", "剪刀" };
System.out.println("请输入您的选择,石头为1,布为2,剪刀为3");
play1 = scanner.nextInt();
play2 = random.nextInt(3) + 1;
System.out.println(play2);
switch (play1 - play2) {
case 0 -> System.out.printf("平局!(你为%s,电脑为%s)", arr[play1 - 1], arr[play2 - 1]);
case -1, 2 -> System.out.printf("你输了!(你为%s,电脑为%s)", arr[play1 - 1], arr[play2 - 1]);
case 1, -2 -> System.out.printf("你赢了!(你为%s,电脑为%s)", arr[play1 - 1], arr[play2 - 1]);
}
Sign in to make a reply
风之子
import java.util.Scanner;
import java.util.Random;
public class Hello {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in); // 创建Scanner对象
Random random = new Random();
int play1, play2;
String[] arr = { "石头", "布", "剪刀" };
System.out.println("请输入您的选择,石头为1,布为2,剪刀为3");
play1 = scanner.nextInt();
play2 = random.nextInt(3) + 1;
System.out.println(play2);
switch (play1 - play2) {
case 0 -> System.out.printf("平局!(你为%s,电脑为%s)", arr[play1 - 1], arr[play2 - 1]);
case -1, 2 -> System.out.printf("你输了!(你为%s,电脑为%s)", arr[play1 - 1], arr[play2 - 1]);
case 1, -2 -> System.out.printf("你赢了!(你为%s,电脑为%s)", arr[play1 - 1], arr[play2 - 1]);
}
}
}