当前位置:网站首页>选择结构(if switch )
选择结构(if switch )
2022-04-22 06:20:00 【Vi---Rum】
IF
使用if语句,如果其中的条件成立,则输出
如果不成立,则现在else语句进行输出
public void test01(){
// 需求:如果今天地震那么我们今天就不上课
// 分析:如果(今天地震) 今天下雨是条件 条件满足 执行不上课
// 程序:if(布尔值){代码} if(boolean){代码}else{代码}
// if(boolean){A}else{B}
// boolean结果为true执行code boolean结果为false不执行
boolean earthquake = false;
if(earthquake){
System.out.println("今天不上课");
}else{
System.out.println("今天上课");
}
System.out.println("程序执行结束");
}
简写方式
public void test02(){
// 需求:
// 如果gender的值为true则dance_style的值为 disco
// 如果gender的值为false则sex的值为 社会摇
boolean gender =false;
String dance_style = gender ==true ? "disco":"社会摇";
System.out.println("舞蹈风格为"+dance_style);
System.out.println("程序执行结束");
}
下面代码中,Random 可以生成随机数
声明一个变量i ,后面便是随机赋值在0~6中的,任意整数
进入if语句,“i”变量会逐条进行执行,当“i”的值等于if中条件的值,则会输出结果
!!注意!!就算条件判断成功,也会进行其他语句的判断
public void test03(){
// 需求:
// 随机生成0到7之间不包含7的整数 0 1 2 3 4 5 6
// 如果这个数字是0 则输出今天是星期日 如实是1则输出星期一 如果是2则输出星期二 以此类推
Random random =new Random();
int i =random.nextInt(7);
System.out.println(i);
if (i==0){
System.out.println("星期日");}
if (i==1) {
System.out.println("星期一");
}
if (i==2){
System.out.println("星期二");}
if (i==3){
System.out.println("星期三");}
if (i==4){
System.out.println("星期四");}
if (i==5){
System.out.println("星期五");}
if (i==6){
System.out.println("星期六");}
System.out.println("程序执行结束");
}
使用if和else语句,可以有效节省运算过程,当“i”的值等于if或者else中的条件会直接输出结果,不再执行下面的判断
public void test04(){
Random random =new Random();
int i =random.nextInt(7);
System.out.println(i);
if (i==0){
System.out.println("星期日");}
else if (i==1) {
System.out.println("星期一");
}
else if (i==2){
System.out.println("星期二");}
else if (i==3){
System.out.println("星期三");}
else if (i==4){
System.out.println("星期四");}
else if (i==5){
System.out.println("星期五");}
else if (i==6){
System.out.println("星期六");
}System.out.println("程序执行结束");
}
switch语句
switch语句,可以直接根据“i”的值,来提供相对应的条件,并输出结果
public void test06(){
// 需求:
// 随机生成0到7之间不包含7的整数 0 1 2 3 4 5 6
// 如果这个数字是0 则输出今天是星期日 如实是1则输出星期一 如果是2则输出星期二 以此类推
Random random = new Random();
int i = random.nextInt(7);
System.out.println(i);
switch (i) {
//case 为其中的条件 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 6:
System.out.println("星期六");
break;
default:
System.out.println("星期日");
break;
}
System.out.println("程序执行结束");
}
简写模式 (适合高版本jdk)
public void test05(){
Random random =new Random();
int i =random.nextInt(7);
System.out.println(i);
switch (i){
case 1-> System.out.println("星期一");
case 2-> System.out.println("星期二");
case 3-> System.out.println("星期三");
case 4-> System.out.println("星期四");
case 5-> System.out.println("星期五");
case 6-> System.out.println("星期六");
default -> System.out.println("星期日");
}
System.out.println("程序执行结束");
}
总结
IF VS SWITCH
IF 语句的执行时间长,空间占有率高。
Switch 语句的执行效率高,空间占有率低。
版权声明
本文为[Vi---Rum]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_54371657/article/details/124120987
边栏推荐
- 驱动与R3的通信 -自定义包
- D. Determine the Photo Position (简单找子串)(2021牛客暑期多校训练营1)
- 1420 · minimum coverage substring II
- Pointer structure const summary
- Redis的设计与实现(3):持久化策略RDB、AOF
- Detailed explanation of linked list exercises
- Could not resolve com.nbsp:library:1.8如何解决
- Codeforces Round #634 (Div. 3)
- Detailed bubble sequence and array name
- 1242 · non overlapping interval
猜你喜欢

Redis的设计与实现(5):主从复制策略和优化

101012分页

SUCTF 2019 EasySQL

Redis的设计与实现(3):持久化策略RDB、AOF

Cache hit rate of sequential table

Ansible的使用

Redis的设计与实现(2):如何处理过期键

Call gate

Leetcode - 2 - (parenthesis generation, longest palindrome string, ring linked list, reverse linked list, nodes in pairwise exchange linked list)

任务段&任务门
随机推荐
A. Alice and Bob (game? Thinking & Violence) (2021 Niuke summer multi school training camp 1)
Recursive reverse linked list
Detailed bubble sequence and array name
Ffmpeg command (VII), combining audio and video into video
L2-001 emergency rescue (extension of shortest Dijkstra - number of shortest paths & maximum weight of paths)
SUCTF 2019 EasySQL
Interrupt gate & Trap gate
Art de la programmation simultanée (9): utilisation finale et principes
Leetcode - 3 - (string addition, maximum number of consecutive 1 < Ⅰ Ⅲ >, maximum difficulty of the exam, deletion of the penultimate node of the linked list)
L1-064 AI core code valued at 100 million (20 points) has wrong format
Codeforces Round #774 (Div. 2)
Links summary qwq
Detailed explanation of linked list exercises
Android Room数据库Like模糊查询
Addition, deletion and search of sequence table (find)
自动化运维之---centos7初始化脚本
L2-002 linked list weight removal (pit of test point 1)
Queue (detailed explanation) -- hand tearing queue exercises
APC(三)
119 · edit distance