1.if嵌套
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50public class IfContianerDemo { public static void main(String[] args) { //if嵌套 Scanner scanner=new Scanner(System.in); System.out.println("请输入姓名:"); String name=scanner.next(); if(name.equals("张三")){ System.out.println("张三,你的事发了"); }else { if(name.equals("李四")){ System.out.println("李四,你完蛋了"); }else { if(name.equals("王五")){ System.out.println("王五,你们三个被逮捕了"); }else { System.out.println("你是良民,走吧"); } } } scanner.close(); } } *if嵌套可以改成简单if-else语句 if(name.equals("张三")){ System.out.println("张三,你的事发了"); }else if(name.equals("李四")){ System.out.println("李四,你完蛋了"); }else if(name.equals("王五")){ System.out.println("王五,你们三个被逮捕了"); }else { System.out.println("你是良民,走吧"); } *if嵌套可以改成简单if语句 if(name.equals("张三")){ System.out.println("张三,你的事发了"); } if(name.equals("李四")){ System.out.println("李四,你完蛋了"); } if(name.equals("王五")){ System.out.println("王五,你们三个被逮捕了"); } if(name.equals("张三")==false&&name.equals("李四")==false&&name.equals("王五")==false){ System.out.println("你是良民,走吧"); }
2 .switch语句
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47public class SwitchDemo { public static void main(String[] args) { Scanner scanner=new Scanner(System.in); System.out.println("请输入成绩:"); float score=scanner.nextFloat(); int level=5; if(score<0){ System.out.println("成绩不能小于0"); }else if(score>90){ level=0; }else if(score>80){ level=1; } else if (score > 70) { level=2; }else if(score>60){ level=3; }else { level=4; } switch(level){ case 0: System.out.println("学神"); break; case 1: System.out.println("学霸"); break; case 2: System.out.println("优"); break; case 3: System.out.println("良"); break; case 4: System.out.println("及格"); break; case 5: System.out.println("学渣"); break; } } } *case后面跟的值,只允许是 基本数据类型: byte short int char 四种 引用数据类型:String 枚举类型 *case后面的值,不能重复 *case穿透:case后面一定要有break,如果没有break,那么程序会一直执行case语句之后所有可以执行的语句,程序会忽略后面case语句,不进行匹配,直接执行后面case中的代码 *switch语句和if语句在部分情况下可以相互转化,如果有非常多的case项,不推荐使用switch语句。
3 .随机数
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
153.1 random对象 Random random=new Random(); //int i=random.nextInt(max-min)+min, 整型随机数 int i=random.nextInt(10);//0~10之间的随机整数 //double d=random.nextDouble()*(max-min)+min,浮点随机数 double d=random.nextDouble()*(34-29)+29; 3.2 Math.random( ) 方法 //浮点型 double d=Math.random()*(max-min)+min //整型 int i=(int)(Math.random()*(max-min)+min)
4 . 作用域
4.1 局部变量
局部变量 | 使用注意 |
---|---|
必须初始化 | 注意声明之后初始化 |
在作用域内不可以重名 | 也就是不可以有第二个相同的变量名 |
5 . while循环
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15//打印100以内 能被4整除,不能被7整除的数据,每⾏打印6个 int count2=0; int j=0; while (count2<=100){ if(count2%4==0&&count2%7!=0){ System.out.print(count2+" "); j++; if(j%6==0){ System.out.println(); } } count2++; }
6 . do-while循环
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17//100 以内 能够被3整除 但是不能被5整除的数 打印输出 public class DoWhileDemo { public static void main(String[] args) { int count=0; int j=0; do{ if(count%3==0&&count%5!=0){ System.out.print(count+"t"); j++; if(j%6==0){ System.out.println(); } } count++; }while (count<100); *do..while循环⽆论条件是否成⽴循环体都会执⾏⼀次
最后
以上就是精明鸡翅最近收集整理的关于day 03 if嵌套、switch语句、while循环的全部内容,更多相关day内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复