Discuss / Java / 打卡打卡

打卡打卡

Topic source

xiao伟iii

#1 Created at ... [Delete] [Delete and Lock User]
package com.itranswarp.learnjava;

import java.util.Scanner;

/**
 * switch实现石头/剪子/布并判断胜负
 */
public class Main {

	@SuppressWarnings("preview")
	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");
		Scanner scanner = new Scanner(System.in);
		// 用户输入:
		int choice = scanner.nextInt();
		// 计算机随机数 1, 2, 3:
		int random = 1 + (int)(Math.random() * 3);
		String result = switch (choice-random) {
		// TODO:
		case 0 -> "Tie!";
		case -1,2 -> "You Win!";
		case -2,1 -> "You Lose!";
		default -> throw new IllegalArgumentException("Unexpected value: " + random);
		};
		System.out.println("You choose: " + choice + ((choice>1) ? ((choice>2) ? " Paper" : " Scissors") : " Rock"));
		System.out.println("Robot choose: " + random + ((random>1) ? ((random>2) ? " Paper" : " Scissors") : " Rock"));
		System.out.println(result);
	}

}

  • 1

Reply