Discuss / Java / 猜拳游戏

猜拳游戏

Topic source

凌翊CJV

#1 Created at ... [Delete] [Delete and Lock User]
import java.util.Scanner;

/** * @author cjv * 测试switch语句 */public class FlowSwitch {
    public static void main(String[] args) {
        // switch实现石头/剪子/布并判断胜负        System.out.println("please choice:");
        System.out.println(" 1: Rock");
        System.out.println(" 2: Scissors");
        System.out.println(" 3: Paper");
        // 用户输入:        Scanner scanner = new Scanner(System.in);
        int choice = scanner.nextInt();
        // 计算机随机数 1, 2, 3:        int random = 1 + (int) Math.random() * 3;
        switch (choice) {
            // TODO:            case 1:
                if (random == 1) {
                    System.out.println("平局");
                }
                else if (random == 2) {
                    System.out.println("胜利");
                }
                else {
                    System.out.println("失败");
                }
                break;
            case 2:
                if (random == 1) {
                    System.out.println("失败");
                }
                else if (random == 2) {
                    System.out.println("平局");
                }
                else {
                    System.out.println("胜利");
                }
                break;
            case 3:
                if (random == 1) {
                    System.out.println("胜利");
                }
                else if (random == 2) {
                    System.out.println("失败");
                }
                else {
                    System.out.println("平局");
                }
                break;
            default:
                System.out.println("请输入正确的数字。");
        }
    }
}

  • 1

Reply