Discuss / Java / 交作业

交作业

Topic source

落落96897

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

import java.util.Scanner;

import java.math.*;

public class Main {

    public static void main(String[] args) {

    String[] names = {"A", "B", "C"};

    int rnumA = (int)(Math.random()*3);//取随机数

    int rnumB = (int)(Math.random()*3);//取随机数

    //String id1 = names[rnum];

    String A = names[rnumA]; 

    String B = names[rnumB]; 

    String game = A+B;

    String tempA = switch(A) {

           case "A"-> "石头";

               case "B"-> "剪刀";

               case "C"-> "布";

               default -> "错误";

           };

         String tempB = switch(B) {

         case "A"-> "石头";

           case "B"-> "剪刀";

           case "C"-> "布";

            default -> "错误";

         };

    System.out.println("A==石头  B==剪刀   C==布\n");

    System.out.println("玩家甲出:"+tempA+"    ||  玩家乙出:"+tempB+'\n');

       String str = switch(game) {

           case "AA","BB","CC" -> "平局";

           case "AB","BC","CA" -> "玩家甲赢了";

           case "AC","BA","CB" -> "玩家乙赢了";

           default -> "错误";

       };

       System.out.println("------------比试结果: "+str+"-----------------");

    }

 }


  • 1

Reply