Discuss / Java / 用的廖老师gitee里的框架写的,也借鉴了一下评论里的答案,刚开始没有想到要用到嵌套的switch-case结构

用的廖老师gitee里的框架写的,也借鉴了一下评论里的答案,刚开始没有想到要用到嵌套的switch-case结构

Topic source

‘’‘java   

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);//Math.random()生成[0,1)之间的随机数,乘以3以后变成0,3),+1后变成[1,4),用(int)强转后变成1,2,3之间某一个数。   

switch (choice) {   

case 1:   

switch (random) {   

case 1:   

System.out.printf("平局,random=%d",random);   

break;   

case 2:   

System.out.printf("你赢了,random=%d",random);   

break;   

case 3:   

System.out.printf("你输了,random=%d",random);   

break;   

}   

break;   

case 2:   

switch (random) {   

case 1:    

System.out.printf("你输了,random=%d",random);   

break;   

case 2:   

System.out.printf("平局,random=%d",random);   

break;   

case 3:   

System.out.printf("你赢了,random=%d",random);   

break;   

}  

break;   

case 3:   

switch (random) {   

case 1:   

System.out.printf("你赢了,random=%d",random);   

break;   

case 2:   

System.out.printf("你输了,random=%d",random);   

break;   

case 3:   

System.out.printf("平局,random=%d",random);   

break;   

}   

}   

}   

}   

'''


  • 1

Reply