日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

第五天学习Java的笔记(if,switch顺序结构)

發(fā)布時間:2025/3/13 java 14 豆豆
生活随笔 收集整理的這篇文章主要介紹了 第五天学习Java的笔记(if,switch顺序结构) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

還有51天

流程概述和順序結(jié)構(gòu)

每條語句的執(zhí)行流程。

1.1 順序結(jié)構(gòu)

//順序結(jié)構(gòu) public class Demo01Sequence {public static void main(String[] args) {System.out.println("1");System.out.println("2");System.out.println("3");//1,2,3} }

1.2 判斷

if語句

//單if語句 public class Demo02If {public static void main(String[] args) {System.out.println("發(fā)現(xiàn)網(wǎng)吧");int age = 19;if (age >= 18) {System.out.println("進入網(wǎng)吧");System.out.println("遇見豬隊友");System.out.println("結(jié)賬走人")}System.out.println("回家");} }

if…else語句

//標準的if-else語句 public classDemo03IfElse {public static void main(String[] args) {int num = 1;if (num % 2 == 0) {//不可以這部分寫成num%2,然后判斷布爾值,會報錯int無法轉(zhuǎn)為booleanSystem.out.println("偶數(shù)");} else {System.out.println("奇數(shù)");}} }

if…else if…else

//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 = 4;int y;if (x >= 3) {y = 2 * x + 1;} else if (x > -1 && x < 3) {y = 2 * x;} else {y = 2 * x - 1;}System.out.println("結(jié)果是:" + y);//9} }

成績劃分練習

/* 90-100優(yōu)秀;80-89好;70-79良;60-69及格;60以下不及格;>100或者<0數(shù)據(jù)錯誤 */ public class Demo05IfElsePratice {public static void main(String[] args) {int score = 97;if (score >= 90 && score <= 100){System.out.println("優(yōu)秀");} else if (score >= 80 && score < 90) {System.out.println("好");} else if (score >= 70 && score < 80) {System.out.println("良");} else if (score >= 60 && score < 70) {System.out.println("及格");} else if (score >= 0 && score < 60) {System.out.println("不及格");} else {System.out.println("數(shù)據(jù)不正確");}} }

用if語句代表三元運算符

//使用三元運算符和標準的if-else語句分別實現(xiàn):取兩個數(shù)字當中的最大值 public class Demo06Max {public static void main(String[] args) {int a = 10,b = 20;//使用三元運算符//int max = a > b ? a : b;//System.out.println("最大值:" + max);if max;if (a > b){max = a;} else {max = b;}System.out.println("最大值:" + max);} }

1.3 選擇

switch語句

public class Demo07Switch {public static void main(String[] args){int num = 1;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 7:System.out.println("星期日");break;default:System.out.println("error");break;//可以省略,但是不建議省略,因為各個case順序可以變,萬一顛倒了default不是最后一個,可能就會穿透其他case}} }

Switch語句的注意事項:

/* 1.多個case后面的數(shù)值不可以重復,報錯case標簽重復 2.switch后面小括號當中只能是下列數(shù)據(jù)類型:基本數(shù)據(jù)類型:byte/short/char/int 引用數(shù)據(jù)類型:String字符串、enum枚舉 3.switch語句格式可以很靈活:前后順序可以顛倒,而且break語句可以省略 “匹配到哪一個case就從哪一個位置向下執(zhí)行,直到遇到了break或者整體結(jié)束為止。” */ public class Demo08SwitchNotice {public static void main(String[] args) {int num = 2;switch (num){case 1:System.out.println(1);break;case 2:System.out.println(2);//break;case 3:System.out.println(3);break;default:System.out.println("error");break;}//會輸出2 3} } 與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖

總結(jié)

以上是生活随笔為你收集整理的第五天学习Java的笔记(if,switch顺序结构)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。