Discuss / Java / My ans

My ans

Topic source

少年__A

#1 Created at ... [Delete] [Delete and Lock User]

package com.itranswarp.learnjava;

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");

// 用户输入:

Scanner scanner = new Scanner(System.in);

int choice = scanner.nextInt();

// 计算机随机数 1, 2, 3:

int random = 1 + (int) (Math.random() * 3);

String out = switch (choice-random) {

// TODO:

case -1, 2 -> "win";

case 1, -2 -> "loose";

default -> "draw";

} ;

System.out.print(out);

}

}


  • 1

Reply