Discuss / Java / 石头剪刀布,你输了就要哭

石头剪刀布,你输了就要哭

Topic source

Yu直男

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

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 = (int)(Math.random() * 3) + 1;

System.out.println(random);

switch (choice) {

case 1 -> {

if(random == 1) {

System.out.println("平局");

}else if(random == 2) {

System.out.println("你赢了");

}else {

System.out.println("你输了就要哭");

}

}

case 2 -> {

if(random == 1) {

System.out.println("你输了就要哭");

}else if(random == 2) {

System.out.println("平局");

}else {

System.out.println("你赢了");

}

}

case 3 -> {

if(random == 1) {

System.out.println("你赢了");

}else if(random == 2) {

System.out.println("你输了就要哭");

}else {

System.out.println("平局");

}

}

default->System.out.println("输入有误,请重试!");

}

}

}


  • 1

Reply