Discuss / Java / 感觉纯粹就是暴力法

感觉纯粹就是暴力法

Topic source

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

// 用户输入:

while (true) {

int choice = 0;

Scanner scanner = new Scanner(System.in);

System.out.print("your choice:");

choice = scanner.nextInt();

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

int a =(int) (Math.random() * 3);

int random = 1 + a;

switch (random) {

case 1 -> System.out.println("your competitor is: " + random + ":Rock.");

case 2 -> System.out.println("your competitor is: " + random + ":Scissors.");

case 3 -> System.out.println("your competitor is: " + random + ":Paper.");

}

switch (choice) {

// TODO:

case 1:

if (random > 1)  {

System.out.println("you fail");

break;

} else if (random == 1)  {

System.out.println("draw, do it again.");

break;

}

case 2:

if (random > 2)  {

System.out.println("you fail");

break;

} else if (random == 2)  {

System.out.println("draw, do it again.");

break;

} else {

System.out.println("you win");

break;

}

case 3:

if (random == 3)  {

System.out.println("draw, do it again.");

break;

} else {

System.out.println("you win");

break;

}

}

}

}

}

switch (choice) {    case 1, 2, 3 -> {        if (random == choice % 3 + 1) {            System.out.println("赢");        } else if (random == choice) {            System.out.println("平");        } else {            System.out.println("输");        }    }    default -> {        System.out.println("wrong");    }}
switch (choice) {
	case 1, 2, 3 -> {
		if (random == choice % 3 + 1) {
			System.out.println("赢");
		} else if (random == choice) {
			System.out.println("平");
		} else {
			System.out.println("输");
		}
	}
	default -> {
		System.out.println("wrong");
	}
}

  • 1

Reply