java学习
day05
前言
流程控制语句
Java语言程序通过控制语句来执行程序流,完成一定的任务。程序流是由若干个语句组成的,语句可以是一条单一的语句,如“c=a+23”。也可以是用花括号{ }括起来的一个复合语句。
目录
- 顺序结构
- 单if语句
- 分支语句
- 循环语句
- 转跳语句
- 课后练习
1、顺序结构
应用:
public class Demo01Sequence{
public static void main(String[] args){
System.out.println(“今天天气不行”);
System.out.println(“在下雨”);
System.out.println(“就要打伞”);
}
}
2、单if语句
应用:
public class Demo02if {
public static void main(String[] args){
System.out.println(“今天阳光明媚,心情不错,正巧看见了一个网吧”);
int age = 16;
if (age >= 18){
System.out.println(“进入网吧玩游戏”);
System.out.println(“遇到强敌,开始不服”);
System.out.println(“心情郁闷,结账走人”);
}
System.out.println(“回家吃饭”);
}
}
3、分支语句
(1)if–else语句
①格式:
//标准的if-else语句
public class Demo03IfElse {
public static void main(String[] args){
int num = 666;
if (num % 20){//如果除以2能够余0,说明是偶数
System.out.println(“偶数”);
}
else{
System.out.println(“奇数”);
}
}
}
②应用
//x和y的关系满足如下:
//如果x >= 3,那么y = 2x + 1;
//如果-1 < x < 3,那么y = 2x;
//如果x <= -1,那么y = 2x - 1;
public class Demo04IfElseExt {
public static void main(String[] args){
int x = -1;
int y ;
if (x >= 3){
y = 2 * x + 1;
}
else if(-1 < x && x < 3){
y = 2 * x;
}
else {
y = 2 * x - 1;
}
System.out.println(“结果是:”+ y);
}
}
③扩展应用:
//题目:使用三元运算符和标准的if-else语句分别实现:取两个数字当中的最大值
public class Demo06Max {
public static void main(String[] args){
int a = 40;
int b = 20;
//首先使用三元运算符
int max1 = a > b ? a : b;
System.out.println(“最大值:”+ max1);
//使用if语句
int max ;
if (a > b){
max = a;
}
else {
max = b;
}
System.out.println(“最大值:”+ max);
}
}
(2)switch语句……(具有穿透性)
①基本应用:
public class Demo07Switch{
public static void main(String[] args){ int day = 18;
int num = day % 7;
switch (num){
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;
case 0:
System.out.println(“星期日”); break;
default:
System.out.println(“数据不合理”); break;//最后一个break语句可以省略,但最好不要省略 }
}
}
②switchnotice语句:
定义: 1.基本数据类型:byte/short/char/int
2.引用数据类型:string字符串、enum枚举
3.switch语句格式可以很灵活:前后顺序可以颠倒,而且break语句还可以省略。
“匹配哪一个case就从哪一个位置向下执行,直到遇到了break或整体结束为止。”
应用:
public class Demo08SwitchNotice{
public static void main(String[] args){
byte num = 3;
switch (num){
case 1:
System.out.println(“你好”);
break;
case 2:
System.out.println(“我好”);
break;
case 3:
System.out.println(“大家好”);
break;
default:
System.out.println(“他好,你也好”);
break;
}// switch
}
}
4、循环语句
(1)for循环:
①定义:
循环结构的基本组成部分,一般可以分为四部分:
1.初始化语句:在循环开始最初执行,而且只做唯一一次。
2.条件判断;如果成立,则循环继续;如果不成立,则循环退出。
3.循环体:重复要做的事情内容,若干行语句。
4.步进语句:每次循环之后都要进行扫尾工作,每次循环结束之后都要执行一次。
②应用:
//使用循环,计算1-100之间的偶数和
public class Demo09For{
public static void main(String[] args){
//++i;
//i++;
int sum=0;
for (int i = 20; i <= 10; i++){
//if(i%21)
sum+=i;
//System.out.println(“我错了,原谅我吧!” + i);
}
//System.out.println(“i”);
System.out.println(“程序停止” + sum);
}
}
(2)while循环:
①扩展格式:
初始化语句:
while (条件判断)
循环体;
步进语句;
②应用:
//题目:用while 求1-100的和
public class Demo10While{
public static void main(String[] args){
for (int i = 1; i <= 5; i++){
System.out.println(“我错了啦!” +i);
}
System.out.println("====================");
int i = 10;//1.初始化语句
while (i <= 5){//2.条件判断
System.out.println(“我错啦!” + i);//3.循环体
i++;//4.步进语句
}
}
}
(3)do–while循环:
①格式:
do-while循环的标准格式:
do{
循环体
}
while(条件判断);
扩展格式:
初始化语句
do{
循环体
步进语句
}
while(条件判断);
②应用:
public class Demo11DoWhile{
public static void main(String[] args){
for (int i=1;i<=5;i++){
System.out.println(“原谅你了!” +i);
}
System.out.println("======================");
int i=100;//初始语句
do {
System.out.println("原谅你了!" +i);//循环体
i++;//步进语句
}
while (i<=10);//条件判断
System.out.println(i);
}
}
③运行结果:
5、跳转语句
(1)break关键字:
①定义:
break关键字的用法有常见的两种:
1.可以用在switch语句中,一旦执行,整个switch语句立刻结束。
2. 还可以用在循环语句中,一旦执行,整个循环语句立刻结束,打断循环。
关于循环的小tip:
凡是次数确定的场景多用for循环;反之多用while循环。
②应用:
public class Demo14Break {
public static void main(String[] args){
for (int i=1; i<=10; i++){
//如果希望从第4次开始,后续中断,就要打断循环。
if (i == 4) //如果当前是第4次
{
break;//那么就打断这个循环
}
System.out.println(“juju”+i);
}
}
}
③运行结果:
(2)continue关键字:
①定义:
continue关键字(循环控制语句)
一旦执行,立刻跳过当前循环剩余内容,马上开始下一次循环。
②应用:
public class Demo15Continue {
public static void main(String[] args){
for (int i = 1;i <= 10; i++)
{
if(i == 4) //如果当前是第4次
continue; //那么跳过当前次循环,马上开始下一次(第5次)
System.out.println(i+“结束了。。。”);
}
}
}
③运行结果:
6、课后练习
tip:
2020080605006