package com.itranswarp.learnjava;
import java.util.Scanner;
/**
* switch实现石头/剪子/布并判断胜负
*/
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println(" 1: Rock");
System.out.println(" 2: Scissors");
System.out.println(" 3: Paper");
System.out.println("please choice:");
int choice = scanner.nextInt();
int random = 1 + (int) Math.random() * 3;
switch (choice) {
case 1 -> {
switch (random) {
case 1 -> System.out.println("平局,再来一次吧!");
case 2 -> System.out.println("你赢了!再玩一局吧!");
case 3 -> System.out.println("你输了!再试一下吧!");
}
case 2 -> {
case 1 -> System.out.println("你输了!再试一下吧!");
case 2 -> System.out.println("平局,再来一次吧!");
case 3 -> System.out.println("你赢了!再玩一局吧!");
case 3 -> {
case 1 -> System.out.println("你赢了!再玩一局吧!");
case 2 -> System.out.println("你输了!再试一下吧!");
case 3 -> System.out.println("平局,再来一次吧!");
default -> System.out.println("只能选择1、2或3哦");
Sign in to make a reply
水晶小三四
package com.itranswarp.learnjava;
import java.util.Scanner;
/**
* switch实现石头/剪子/布并判断胜负
*/
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println(" 1: Rock");
System.out.println(" 2: Scissors");
System.out.println(" 3: Paper");
System.out.println("please choice:");
int choice = scanner.nextInt();
int random = 1 + (int) Math.random() * 3;
switch (choice) {
case 1 -> {
switch (random) {
case 1 -> System.out.println("平局,再来一次吧!");
case 2 -> System.out.println("你赢了!再玩一局吧!");
case 3 -> System.out.println("你输了!再试一下吧!");
}
}
case 2 -> {
switch (random) {
case 1 -> System.out.println("你输了!再试一下吧!");
case 2 -> System.out.println("平局,再来一次吧!");
case 3 -> System.out.println("你赢了!再玩一局吧!");
}
}
case 3 -> {
switch (random) {
case 1 -> System.out.println("你赢了!再玩一局吧!");
case 2 -> System.out.println("你输了!再试一下吧!");
case 3 -> System.out.println("平局,再来一次吧!");
}
}
default -> System.out.println("只能选择1、2或3哦");
}
}
}