1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- import java.util.*;
- class TestSwitch06Ex
- {
- public static void main(String[] args)
- {
- /*
- 随机产生3个1-6的整数,如果三个数相等,那么称为“豹子”,如果三个数之和大于9,称为“大”,如果三个数之和小于等于9,称为“小”,用户从键盘输入押的是“豹子”、“大”、“小”,并判断是否猜对了
- 提示:随机数 Math.random()产生 [0,1)范围内的小数
- 如何获取[a,b]范围内的随机整数呢?(int)(Math.random() *a) + b
-
- */
- //随机数
- int a = (int)(Math.random() *5) + 1;
- int b = (int)(Math.random() *5) + 1;
- int c = (int)(Math.random() *5) + 1;
- //标志位
- boolean flag = false;
- Scanner sc = new Scanner(System.in);
- //数字
- System.out.println("输入“豹子”, “大”,“小”");
-
- String str = sc.next();
- //判断合理性
-
- switch(str){
- case "豹子":
- if (a == b && b == c ){
- flag = true;
- }
- break;
- case "大":
- if ((a + b + c) > 9 ){
- flag = true;
- }
- break;
- case "小":
- if ((a + b + c) < 9 ){
- flag = true;
- }
- break;
- }
- if (flag){
- System.out.println("abc="+a+"-"+b+"-"+c+":猜对了");
- }else{
- System.out.println("abc="+a+"-"+b+"-"+c+":猜错了");
- }
- }
- }
|