Discuss / Java / 代码

代码

Topic source

可姆可汗

#1 Created at ... [Delete] [Delete and Lock User]

import java.util.Scanner;

/**

 * switch实现石头/剪子/布并判断胜负

 */

public class Main {

public static void main(String[] args) {

System.out.println("please choice:");

System.out.println(" 1: Rock");

System.out.println(" 2: Scissors");

System.out.println(" 3: Paper");

var sc = new Scanner(System.in);

while (true) {

// 用户输入:

int choice = sc.nextInt();

// 计算机随机数 1, 2, 3:

int random = 1 + (int)(Math.random() * 3);

System.out.println(random);

switch (choice) {

case 1 -> System.out.println(random == 1 ? "draw" : (random == 2 ? "win" : "lose"));

case 2 -> System.out.println(random == 2 ? "draw" : (random == 3 ? "win" : "lose"));

case 3 -> System.out.println(random == 3 ? "draw" : (random == 1 ? "win" : "lose"));

default -> System.out.println("Please input correct option!");

}

}

}

}


  • 1

Reply