Discuss / Java / 1

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 input=new Scanner(System.in);

        int choice = 0;

        choice=input.nextInt();

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

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

        switch (choice) {

        case 1:

        System.out.println("your choice is Rock");

            switch(random){

            case 1:

                System.out.println("draw");

                break;

            case 2:

                System.out.println("win");

                 break;

            case 3:

                System.out.println("lose");

                 break;

                }

        break;

        case 2:

        System.out.println("your choise is Scissors");

            switch(random){

            case 1:

                System.out.println("lose");

                 break;

            case 2:

                System.out.println("draw");

                 break;

            case 3:

                System.out.println("win");

                 break;

                }

                 break;

        case 3:

        System.out.println("your choice is paper");

        switch (random){

        case 1:

        System.out.println("win");

         break;

        case 2:

        System.out.println("lose");

         break;

        case 3:

        System.out.println("draw");

         break;

        }

         break;

        }

    }

}


  • 1

Reply