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!");
}
Sign in to make a reply
可姆可汗
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!");
}
}
}
}