Discuss / Java / 1

説025

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

import java.util.*;

/**

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

System.out.print("开始游戏,你的选择是:");

int option = scanner.nextInt();//玩家输入

int random =(int) (Math.random()*(2)+1); //计算机输入

int s = option-random;//通过玩家输入的值与电脑输入的值的差来判断输赢

switch (s) {

case 1:

System.out.println("win");

break;

case 2:

System.out.println("lost");

break;

case 0:

System.out.println("tie");

break;

case -1:

System.out.println("lost");

break;

default:

System.out.println("win");

}

    }

}


  • 1

Reply