//通过套用switch-case实现判断
import java.util.Scanner; /** * switch实现石头/剪子/布并判断胜负 */ public class Main { @SuppressWarnings("preview") 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"); // 用户输入 1, 2, 3: Scanner in = new Scanner(System.in); System.out.print("Please input your choice:"); int choice = in.nextInt(); // 计算机随机数 1, 2, 3: int random = (int) (1+Math.random() * 3); System.out.println("Your rival:" + random); // 判断与输出 switch (choice) { case 1 -> { switch (random) { case 1 -> System.out.println("Result:Draw"); case 2 -> System.out.println("Result:Win"); case 3 -> System.out.println("Result:Lose"); default -> System.out.println("Error"); } } case 2 -> { switch (random) { case 1 -> System.out.println("Result:Lose"); case 2 -> System.out.println("Result:Draw"); case 3 -> System.out.println("Result:win"); default -> System.out.println("Error"); } } case 3 -> { switch (random) { case 1 -> System.out.println("Result:Win"); case 2 -> System.out.println("Result:Lose"); case 3 -> System.out.println("Result:Draw"); default -> System.out.println("Error"); } } default -> System.out.println("Error"); } } }
Sign in to make a reply
YANGZY1202
//通过套用switch-case实现判断