Discuss / Java / 作业

作业

Topic source

寒蔦

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

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

// 用户输入:

Scanner scanner = new Scanner(System.in);

int choice = scanner.nextInt();

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

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

System.out.println(random);

switch (choice) {

// TODO:

case 1 -> {

System.out.println("你出了Rock");

switch (random) {

case 1 -> System.out.println("我也是Rock,平局");

case 2 -> System.out.println("我是 Scissors,你赢了");

case 3 -> System.out.println("我是Paper,你输了");

}

}

case 2 -> {

System.out.println("你出了Scissors");

switch (random) {

case 1 -> System.out.println("我是Rock,你输了");

case 2 -> System.out.println("我也是Scissors,平了");

case 3 -> System.out.println("我是Paper,你赢了");

}

}

case 3 -> {

System.out.println("你出了 Paper");

switch (random) {

case 1 -> System.out.println("我是Rock,你赢了");

case 2 -> System.out.println("我也是Scissors,你输了");

case 3 -> System.out.println("我也是Paper,平了");

}

}

}

}

}


  • 1

Reply