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) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("你要出什么?:");
        System.out.println(" 1: 石头");
        System.out.println(" 2: 剪刀");
        System.out.println(" 3: 布");
        // 用户输入:        int choice = scanner.nextInt();
        // 计算机随机数 1, 2, 3:        int random = 1 + (int)(Math.random() * 3);
        System.out.println("计算机出的是"+random);
        switch (random) {
            // TODO:            case 1 :
                switch (choice){
                    case 1 :
                        System.out.println("平局");
                        break;
                    case 2 :
                        System.out.println("你输了");
                        break;
                    case 3 :
                        System.out.println("你赢了");
                        break;
                }
                break;
            case 2 :
                switch (choice){
                    case 1 :
                        System.out.println("你赢了");
                        break;
                    case 2 :
                        System.out.println("平局");
                        break;
                    case 3 :
                        System.out.println("你输了");
                        break;
                }
                break;
            case 3 :
                switch (choice){
                    case 1 :
                        System.out.println("你输了");
                        break;
                    case 2 :
                        System.out.println("你赢了");
                        break;
                    case 3 :
                        System.out.println("平局");
                        break;
                }
                break;
        }
    }

}

  • 1

Reply