Discuss / Java / 答案

答案

Topic source

缪哈桑

#1 Created at ... [Delete] [Delete and Lock User]
public class Main {    public static void main(String[] args) {        Scanner scanner = new Scanner(System.in); // 创建Scanner对象        System.out.print("剪刀为1,石头为2,布为3:"); // 打印提示        var choice = scanner.nextInt();        Random rd = new Random();        var rand_num = rd.nextInt(1, 4);        String opt = switch (choice) {            case 1 -> rand_num == 1 ? "平局" : (rand_num == 2 ? "输" : "胜");            case 2 -> rand_num == 2 ? "平局" : (rand_num == 3 ? "输" : "胜");            case 3 -> rand_num == 3 ? "平局" : (rand_num == 1 ? "输" : "胜");            default -> "输入异常";        };        System.out.println("结果" + opt);    }}

优雅


  • 1

Reply