Discuss / Java / 来了

来了

Topic source
import java.util.Scanner;

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

	public static void main(String[] args) {
		System.out.println("please choice:");
		System.out.println(" 1: 石头");
		System.out.println(" 2: 剪刀");
		System.out.println(" 3: 布");
		Scanner scanner=new Scanner(System.in);
		int choice=scanner.nextInt();
		// 用户输入:
		// 计算机随机数 1, 2, 3:
		int random =(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("我出布,你输了···");
		    }
		}
		    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