if _ else if _ else,case,程序逻辑判断- java基础

//单个判端

if(){

}

//双判端

if(){

}else{

}

//多重判端

if(){

}else if(){

}else if(){

}else{

}

 1 package test1;
 2 // 学习的是 判端掷色子游戏 1-6   if , else if , else 判端方法  
 3 public class TestRandom {
 4     public static void main (String[] args) {
 5         double d = Math.random();
 6         int e = 1+(int)(d*6);//1到6之间的随机数,int强制转型
 7         
 8         System.out.println(e + "\n");
 9         // 测试if 判断一层
10         if(e>4) {
11             System.out.println("大于4都会打印\n");
12         }
13         // 测试双重判断
14         if(e>=3) {
15             System.out.println("大数4~6\n");
16         }else {
17             System.out.println("小数1~3\n");
18         }
19         
20         System.out.println("=====多重判断怎么写?=====\n");
21         if(e==6) {
22             System.out.println("运气不错,加倍!!");
23         }else if(e>=4) {
24             System.out.println("运气还可以哦!");
25         }else if(e>2) {
26             System.out.print("运气一般,再接再励!");
27         }else {
28             System.out.println("运气差的很,回家洗洗睡吧!");
29         }
30     }
31 }
if(){}else{}

case 是在判端条件等于的情况下使用

比如:a = 数据数 1~5

if(a==1){

}else(a==2){

}

这种情况下采用case 这种方式写

 1 package test1;
 2 // 学习的是 判端掷色子游戏 1-6   if , else if , else 判端方法  
 3 public class TestSwitch {
 4     public static void main (String[] args) {
 5         double d = Math.random();
 6         int e = 1+(int)(d*6);//1到6之间的随机数,int强制转型
 7         
 8         System.out.println(e);
 9         if(e==6) {
10             System.out.println("运气不错,加倍!!");
11             
12         }else if(e==5) {
13             System.out.println("运气还可以哦!");
14             
15         }else if(e==4){
16             System.out.println("运气一般,再接再励!");
17         }else {
18             System.out.println("运气差的很,回家洗洗睡吧!");
19         }
20         
21         System.out.println("###############################");
22         //switch 语句与前面的 判读一模一样,在作等于判断时使用,结构比较清晰,但是不能忘了break;
23         //不然执行完成会继续执行不退出。弄断的意思。
24         switch(e) {
25         case 6:
26             System.out.print("运气不错,加倍!!");
27             break;
28         case 5:
29             System.out.println("运气还可以哦!");
30             break;
31         case 4:
32             System.out.println("运气一般,再接再励!");
33             break;
34         default:
35             System.out.println("运气差的很,回家洗洗睡吧!");
36             break;
37         }
38         
39         System.out.println("下面这个例子恰恰使用了case 穿透");
40         char c = 'a';
41         int rand = (int) (26*Math.random());
42         char c2 = (char)(c + rand);
43         System.out.println(c2 + ": ");
44         switch (c2) {
45         case 'a':
46         case 'e':
47         case 'i':
48         case 'o':
49         case 'u'://等于a,e,i,o,u  的情况下  没找到break 就一直运行 
50             System.out.println("元音");
51             break;
52         case 'y':
53         case 'w':
54             System.out.println("半元音");
55             break;
56         default:
57             System.out.println("辅音");
58         
59         }
60 
61         }
62 }
case

猜你喜欢

转载自www.cnblogs.com/zhenyu1/p/10703201.html