300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > java的if if else while do while switch for执行语句

java的if if else while do while switch for执行语句

时间:2020-10-22 09:52:30

相关推荐

java的if  if else while do while switch for执行语句

一、java执行语句

分类:

1.顺序语句:方法中的代码从上往下执行

2.分支语句:根据不同的条件,指定不同的功能

2.1 if分支

2.2 switch分支

3.循环语句:条件成立,就重复性的执行某个功能

3.1 for循环

3.2 while循环

3.3 do-while循环

4.特殊的流程控制语句

4.1 break

4.2 continue

4.3 return

4.4 lable

1、if分支

1.1简单的if分支

语法结构:

if(表达式){

…代码块…

}

理解:表达式的结果必须是boolean类型

true - 执行代码块

false - 跳出if分支

实验1:

import java.util.Scanner;public class Test01{/**案例:如果你的Java考试成绩大于98分,你就能获得一辆法拉利作为奖励*/public static void main(String[] args){Scanner scan = new Scanner(System.in);System.out.println("请输入成绩:");double score = scan.nextDouble();if(score > 98){System.out.println("奖励你法拉利玩具赛车一辆");}}}

实验2:

import java.util.Scanner;public class Test02{/**知识点:复杂的if分支总结:1.if可以判断区间2.if可以判断复杂的条件3.变量的驼峰命名法:除了第一个单词,其余的单词首字母大写*/案例:你Java成绩大于98分,而且音乐成绩大于80分,老师奖励他;或者Java成绩等于100分,音乐成绩大于70分,老师也可以奖励他。Scanner scan = new Scanner(System.in);System.out.println("请输入Java成绩:");double javaScore = scan.nextDouble();System.out.println("请输入音乐成绩:");double musicScore = scan.nextDouble();if((javaScore>98&&musicScore>80) || (javaScore==100&&musicScore>70)){System.out.println("奖励你一根棒棒糖");}}}

1.2、if else 分支

语法结构:

if(表达式){

…代码块…

}else{

…else代码块…

}

理解:

表达式的结果必须是boolean

true - 执行代码块

false- 执行else代码块

import java.util.Scanner;public class test3{//public static void main(String[] args){Scanner scan = new Scanner(System.in);System.out.println("请输入java成绩:");double score = scan.nextDouble();if(score>98){System.out.println("恭喜你合格了!!");}else{System.out.println("很遗憾你不合格");}}}

1.3、多重if else分支

语法结构:

​ if(表达式1) {

​ 代码块1;

​ }else if(表达式2) {

代码块2;

​ }else if(表达式n) {

​ 代码块n;

​ }else {

​ else代码块;

​ }

import java.util.Scanner;public class test3{/**案例一:人类的健康值在15-20的数值内为偏瘦20-25的数值内为健康25-30的数值内为偏胖(健康值算法为:体重(Kg) / 身高(米)的平方)*/public static void main(String[] args){Scanner scan = new Scanner(System.in);System.out.println("请输入体重:");double wight = scan.nextDouble();System.out.println("请输入身高:");double hight = scan.nextDouble();double health=wight/(hight*hight);if(health>15 && health<=20){System.out.println("你的体重偏瘦!!");}else if(health>20 && health<=25){System.out.println("你的身体挺健康!!");}else if(health>25 && health<=30){System.out.println("你的身体偏重");}else{System.out.println("健康异常,请到医院检查");}}}

1.4、if的嵌套

import java.util.Scanner;public class Test05{/**知识点:嵌套if分支案例:举行运动会,百米赛跑跑入16秒内的学生有资格进决赛,根据性别分别进入男子组和女子组。*/public static void main(String[] args){Scanner scan = new Scanner(System.in);System.out.println("请输入成绩:");double score = scan.nextDouble();if(score > 0 && score < 16){System.out.println("请输入性别:");String sex = scan.next();//输入字符串if(sex.equals("男")){//判断sex和"男"这两个字符串内容是否相等System.out.println("恭喜进入到男子组决赛");}else if(sex.equals("女")){//判断sex和"女"这两个字符串内容是否相等System.out.println("恭喜进入到女子组决赛");}else{System.out.println("性别异常");}}else if(score >= 16){System.out.println("重在参与");}else{System.out.println("成绩异常");}/**总结if:语法的区别:if(){}:最简单的ifif...else...:二选一if...else if...:多选一if可以无限层嵌套应用场景:可以判断单个值可以判断区间可以判断复杂的条件*/}}

2、switch语句

知识点:switch分支

语法结构:

switch(表达式){

case 值1:

…代码块1…

break;

case 值2:

…代码块2…

break;

case 值n:

…代码块n…

break;

default:

…default代码块…

break;

}

理解:

表达式的结果可以是:byte、short、int、枚举(JDK1.5)、String(JDK1.7)

分别和值1、值2、值n比较,哪个相等,就执行对应的代码块

break:跳出switch分支语句

default代码块类似于else{},是其余其他情况的意思,根据需求可写可不写

扩展:

​ 1、case的值可以相同吗?不可以

​ 2、default可以省略吗?可以

​ 3、break可以省略吗?可以

​ 4、default的位置一定要在最后吗?不一定

​ 5、case后面值的类型可以不一样吗?可以不一样,但是必须兼容

​ 6、表达式值的类型可以是什么?byte、short、int、枚举(JDK1.5)、String(JDK1.7)

​ 7、switch表达式的类型为什么只有byte、short、int、枚举(JDK1.5)、String(JDK1.7)?

​ switch表达式的结果在底层只接受int

​ byte自动转型成int

​ short自动转型成int

​ 枚举的对象系统会给他赋int值

​ String是获取的ASCII码

import java.util.Scanner;public class Test06{/**案例:switch(50){case 10:System.out.println("10");break;case 50:System.out.println("50");break;case 100:System.out.println("100");break;default:System.out.println("...default代码块...");break;}System.out.println("switch分支以外的代码");案例:你参加计算机编程大赛如果获得第一名,马尔代夫旅游如果获得第二名,将奖励苹果Pro笔记本电脑一部如果获得第三名,将奖励移动硬盘一个否则,谢谢参与*/public static void main(String[] args){Scanner scan = new Scanner(System.in);System.out.println("请输入比赛名次:");String str = scan.next();switch(str){case "第一名":System.out.println("马尔代夫旅游");break;case "第二名":System.out.println("奖励苹果Pro笔记本电脑一部");break;case "第三名":System.out.println("奖励移动硬盘一个");break;default:System.out.println("谢谢参与");break;}}}

3、switch语句里嵌套if

​ if vs switch

​ 语法结构的区别:

​ if的表达式:boolean

​ switch的表达式:byte、short、int、枚举(JDK1.5)、String(JDK1.7)

​ 应用场景的区别:

​ if:判断单个值、区间、复杂的条件

​ switch:判断单个值

import java.util.Scanner;public class test7{///**知识点:深入switch分支需求:输入年和月,输出当月的天数闰年:被4整除且不能被100整除 或者 被400整除的年份*/public static void main(String[] args){Scanner scan = new Scanner(System.in);System.out.print("请输入年份:\n");int year = scan.nextInt();System.out.print("请输入月份:\n");int month = scan.nextInt();int day = 0;switch(month){case 1:case 3:case 5:case 7:case 8:case 10 :case 12:day =31;break;case 4:case 6:case 9:case 11:day =30;break;case 2:if(year%4==0&&year%100!=0 || year%400==0){day=29;}else{day=28;}break;}System.out.print(year + "年" + month+"月的天数是"+ day +"天");}}

4、for循环

知识点:for循环

​ 含义:条件成立就重复执行

​ 好处:减少了代码的冗余(减少重复性的代码)

​ 语法结构:

​ for(表达式1;表达式2;表达式3){

​ …代码块…

​ }

​ 理解:

​ 表达式1:初始化变量

​ 表达式2:判断条件

​ 表达式3:更新变量

​ 执行流程:

​ 1.初始化变量

​ 2.判断条件:判断条件的结果必须是boolean

​ 2.1 true - 执行代码块,并更新变量,重复第2个步骤

​ 2.2 false- 跳出整个循环语句

​ for循环变形记:

​ 循环中声明变量的作用域只能在循环内

​ 再循环中i++和++i没有区别

import java.util.Scanner;public class Test09{public static void main(String[] args){/**int i = 1;for(;i<=5;){System.out.println("hhhhhh");//i++;++i;}System.out.println(i);理解案例1:输出1-9的数字for(int i = 1;i<=9;i++){System.out.println(i);}理解案例2:输出0-9的数字for(int i = 0;i<10;i++){System.out.println(i);}理解案例3:输出1~9数字中的奇数//解决方案1for(int i = 1;i<10;i+=2){System.out.println(i);}//解决方案2for(int i = 1;i<10;i++){if(i%2!=0){System.out.println(i);}}理解案例4:输出9~1的数字for(int i = 9;i>0;i--){System.out.println(i);}总结 - 理解案例:i可以从1开始也可以从0开始,更新变量可以递增也可以递减死循环:(应该避免的)for(;;){System.out.println("哈哈哈哈哈哈哈嗝");}伪死循环:(应该避免的)for(int i = 0;i>=0;i++){//当输出达到int类型的最大范围就跳出System.out.println("哈哈哈哈哈哈哈嗝");}案例1:循环录入5次int类型的数字,输出总和Scanner scan = new Scanner(System.in);int sum = 0;//总和for(int i = 1;i<=5;i++){System.out.println("请输入第" + i + "个数字:");int num = scan.nextInt();sum += num;//累加}System.out.println("总和为:" + sum);案例2:循环录入一个同学的5门课成绩,计算平均分Scanner scan = new Scanner(System.in);double sum = 0;//总成绩for(int i = 1;i<=5;i++){System.out.println("请输入第" + i + "门成绩:");double score = scan.nextDouble();sum += score;//累加}System.out.println("平均分为:" + (sum/5));案例3:循环录入5次int类型的数字,输出最大值Scanner scan = new Scanner(System.in);System.out.println("请输入第1个数字:");int max = scan.nextInt();//假设第一个输入的数字为最大值for(int i = 2;i<=5;i++){System.out.println("请输入第" + i + "个数字:");int num = scan.nextInt();if(max < num){max = num;}}System.out.println("最大值为:" + max);案例4:打印图形*************/for(int i = 0;i<3;i++){for(int j = 0;j<4;j++){System.out.print("*");}System.out.println();}}}

4.1、for循环嵌套

public class Test01{public static void main(String[] args){/**知识点:for循环嵌套需求:打印图形***************for(int i = 0;i<5;i++){for(int j = 0;j<=i;j++){System.out.print("*");}System.out.println();}***************for(int i = 0;i<5;i++){for(int k = 0;k<i;k++){System.out.print(" ");}for(int j = 5;j>i;j--){System.out.print("*");}System.out.println();}*********for(int i = 0;i<3;i++){for(int k = 2;k>i;k--){System.out.print(" ");}for(int j = 0;j<i*2+1;j++){System.out.print("*");}System.out.println();}** ******方案一:for(int i = 0;i<3;i++){for(int k = 2;k>i;k--){System.out.print(" ");}for(int j = 0;j<i*2+1;j++){if(i==0 || i==2 || j==0 || j==i*2){System.out.print("*");}else{System.out.print(" ");}}System.out.println();}方案二:for(int i =0;i<3;i++){switch(i){case 0:case 2:for(int k=2;k>i;k--){System.out.print(" ");}for(int j=0;j<2*i+1;j++){System.out.print("*");}break;case 1:for(int h=0;h<2;h++){System.out.print(" *");}break;}System.out.println();}方案三:for(int i = 0;i<3;i++){for(int k = 2;k>i;k--){System.out.print(" ");}for(int j = 0;j<i*2+1;j++){if(i==1&&j=i){System.out.print("*");}else{System.out.print(" ");}}System.out.println();}需求:九九乘法表*/for(int i = 1;i<=9;i++){for(int j = 1;j<=i;j++){System.out.print(j + "x" + i + "=" + (i*j) + "\t");}System.out.println();}}}

5、while 循环

​ 知识点:while循环

​ 语法结构:

​ while(表达式){

​ …代码块…

​ }

​ 理解:

​ 表达式的结果必须是boolean

​ true - 执行代码块

​ false- 跳出循环

​ 总结:循环次数不确定

public class Test02{public static void main(String[] args){案例:我有个梦想,每月存3000,每年递增1000元,多少个月后存满20万*/int money = 3000;int allMoney = 0;int month = 0;while(allMoney < 200000){allMoney+=money;month++;if(month % 12 == 0){money+=1000;}}System.out.println(month + "个月以后存满20万 " + money);}}

6、do-while循环

知识点:do-while循环

​ 语法结构:

​ do{

​ …代码块…

​ }while(表达式);

​ 理解:

​ 表达式的结果必须是boolean

​ true - 执行代码块

​ false- 跳出循环

​ 执行顺序:

​ 1.先执行一遍代码块

​ 2.判断表达式

​ 2.1 true - 执行代码块,再重复第2个步骤

​ 2.2 false- 跳出循环

import java.util.Scanner;public class Test03{public static void main(String[] args){Scanner scan = new Scanner(System.in);System.out.println("猜一猜作者是谁");String str;do{System.out.println("窗前明月光");System.out.println("疑是地上霜");str = scan.next();}while(str.equals("李白"));}}

7、for Vs while Vs do-while

​ 语法结构的区别:

​ for(初始化变量;判断条件;更新变量){}

​ while(判断条件){}

​ do{}while(判断条件);

​ 循环共同点:判断条件的结果都是boolean值,true-循环 false-跳出循环

​ 执行顺序的区别:

​ for:先判断,再执行

​ while:先判断,再执行

​ do-while:先执行一遍,再判断

​ 应用场景的区别:

​ 循环次数确定:for

​ 循环次数不确定,先判断,再执行:while

​ 循环次数不确定,先执行一遍,再判断:do-while

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。