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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java6:流程控制

發布時間:2023/12/19 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java6:流程控制 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Java 流程控制:

????順序

????分支

????循環


分支:

????

????if(布爾表達式){

????????語句塊

????}else{

????????語句塊

????}


????盡量使用肯定條件,減少else,減少嵌套

package?day06; import?java.util.Scanner; public?class?Demo01?{public?static?void?main(String[]?args)?{Scanner?con?=?new?Scanner(System.in);System.out.print("輸入一個整數:");int?n?=?con.nextInt();con.close();if(n?>?0){System.out.println("是正整數");}else{System.out.println("不是正整數");}} }package?day06; import?java.util.Scanner; public?class?Demo01?{public?static?void?main(String[]?args)?{Scanner?con?=?new?Scanner(System.in);System.out.print("輸入一個整數:");int?n?=?con.nextInt();con.close();if(n?>?0)//if語句塊中只有一行代碼的時候,可以省略花括號System.out.println("是正整數");elseSystem.out.println("不是正整數");} }package?day06; import?java.util.Scanner; public?class?Demon02?{public?static?void?main(String[]?args)?{Scanner?con?=?new?Scanner(System.in); System.out.print("輸入一個整數:");int?n?=?con.nextInt();con.close();if(n?>?0)System.out.println("正整數");else?if(n?==?0)System.out.println("是0");elseSystem.out.println("是負整數"); } }


????switch - case 結構

????性能十分優秀?

????但是 只能 根據整數參數進行分支操作

????if 多路 嵌套 條件靈活 適應性廣,性能相對于 switch 差

????

????如果 是根據整數多路分支 用switch?

????其他的可以使用 if

????switch(command){

????case 1:

????????//語句

????????break;

????case 2:

????????//語句

????????break;

????case 3:?

????????? ?

? ? ? ? ?// 3和4用同一個語句

? ? ?case 4:?

????????//語句? ?

????????break;

????}


package?day06; import?java.util.Scanner; public?class?Demo03?{public?static?void?main(String[]?args)?{Scanner?con?=?new?Scanner(System.in);System.out.print("輸入成績:");int?score?=?con.nextInt(); con.close();if(score?>=?0){String?level;switch(score/10){case?10:?level?=?"學霸"; break;case?9:level?=?"學神";break;case?8:level?=?"學民";break;case?7:level?=?"學痞";break;case?6:level?=?"學渣";break;default:level?=?"渣渣"; }System.out.println("你好"+level);}elseSystem.out.println("渣渣有負的嗎?"); } }



????循環結構


????經典的for常用于計次循環

????for 循環可以替代其他他的循環結構

????for(表達式1;表達式2;表達式3){

????????循環體

????}

????for(初始化表達式;循環條件(boolean);遞增表達式){

????????循環體

????}


????for(;;){} ==》死循環

????? ? 在不知道用什么循環的時候就用死循環


????for(;循環條件;)當循環

????????控制使用


? ? ?continue?

????用在循環中,結束本次循環而開始下一次循環.


????break

????用于退出當前語句塊,退出循環

????????在嵌套的循環結構中,break 用于退出所在循環體,如果要退出外層循環體,需要使用標號的方式

????????for(){

????????????for(){

????????? ?????break;

????????????}

????????}

????????

????????outer:for(){

????????????for(){

????????? ?????break outer;

????????????}

????????}

package?day06; /** *?99乘法表 */ public?class?Demo04?{public?static?void?main(String[]?args)?{for(int?x?=?1;x?<?10;x++){if(x?==?2)continue;for(int?y?=?1;?y?<=?x;y++){if(y?==?6)?break;if(x*y?>=?10)System.out.print(y+"*"+x+"="+x?*?y+'?');elseSystem.out.print(y+"*"+x+"="+"0"+x?*?y+'?'); }System.out.println();}} }package?day06; /** *用死循環,反轉字數 */public?class?Demo05?{public?static?void?main(String[]?args)?{int?num?=?1234567890;int?sum?=?0;int?last;for?(;;)?{last?=?num?%?10;sum?=?sum?*?10?+?last;num?/=?10;if?(num?==?0)break;}System.out.println(sum);} }package?day06;import?java.util.Scanner;public?class?Demo06?{public?static?void?main(String[]?args)?{Scanner?con?=?new?Scanner(System.in);int?score?=?-1;for?(;?score?<?0?||?score?>?100;)?{System.out.print("輸入分數:");score?=?con.nextInt();}System.out.print(score);} }

????while 循環

????先判斷 后循環

????while(循環條件(布爾)){

????????循環體

????}

package?day06;import?java.util.Scanner;public?class?Demo07?{public?static?void?main(String[]?args)?{Scanner?con?=?new?Scanner(System.in);System.out.print("請輸入一個三位數:");int?num?=?con.nextInt();int?nu?=?num;int?sum?=?0;while?(num?!=?0)?{int?last?=?num?%?10;sum?+=?last?*?last?*?last;num?/=?10;}if?(sum?==?nu)System.out.println(nu?+?"是水仙花數");elseSystem.out.println(nu?+?"不是水仙花數");} }

????

?

package?day06;import?java.util.Scanner;public?class?Demo07?{public?static?void?main(String[]?args)?{//?Scanner?con?=?new?Scanner(System.in);//?System.out.print("請輸入一個三位數:");//?int?num?=?con.nextInt();for?(int?num?=?100;?num?<?1000;?num++)?{int?nu?=?num;int?sum?=?0;while?(nu?!=?0)?{int?last?=?nu?%?10;sum?+=?last?*?last?*?last;nu?/=?10;}if?(sum?==?num)System.out.println(num?+?"是水仙花數");elseSystem.out.println(num?+?"不是水仙花數");}} }

???

????

????

????do while 循環

? ? ?先循環在檢查,無論boolean表達式的值是true 還是 false 最少會進行一次循環

????do{

????? 循環體

????}while(循環條件)


????

????對比一下: for ;while ;do while


????for 常用于 經典使用方式 與固定次數有關循環處理

????for 可以替代其他的兩種循環

????????for(;循環條件;){} 替代 while(循環條件){}

????????死循環:for(;;){} 替代while(true){}

????????for{;;}{if(循環條件) break;} 替代 do while(循環條件)

????while 循環用于在循環體開始時候判斷循環條件

????do while 循環用于在循環體結束時候判斷循環條件

????不知道用什么循環就要for 的死循環,在適當的條件時候使用break?




































轉載于:https://blog.51cto.com/lmdtx/1698843

總結

以上是生活随笔為你收集整理的java6:流程控制的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。