import java.util.Scanner; /** * switch实现石头/剪子/布并判断胜负 */ public class Main { public static void main(String[] args) { String[] mora = {"石头", "剪刀", "布"}; System.out.println("请选择:"); System.out.printf(" 1: %s\n", mora[0]); System.out.printf(" 2: %s\n", mora[1]); System.out.printf(" 3: %s\n", mora[2]); // 用户输入: Scanner scanner =new Scanner(System.in); int choice; choice = scanner.nextInt(); if (choice > 3 || choice < 1) { System.out.println("请输入正确的数字。"); return; } // 计算机随机数 1, 2, 3: int random = 1 + (int) Math.random() * 3; random = random % 3; System.out.printf("我出的是%s, 所以", mora[random-1]); switch (choice) { case 1: if (random == 1) System.out.println("平局!"); else if(random == 2) System.out.println("你赢了!"); else System.out.println("你输了!"); break; case 2: if (random == 2) System.out.println("平局!"); else if(random == 3) System.out.println("你赢了!"); else System.out.println("你输了!"); break; case 3: if (random == 3) System.out.println("平局!"); else if(random == 1) System.out.println("你赢了!"); else System.out.println("你输了!"); break; default: break; } } }
Sign in to make a reply
Dereen