生活随笔
收集整理的這篇文章主要介紹了
顺序结构,选择结构,反编译
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
順序結構
- Java基本結構就是順序結構,除非特別指明,否則就按照順序一句一句的執行
- 順序結構是最簡單的算法結構
- 任何一個算法都離不開的一種基本算法結構
package com.boss.struct;public class ShunXuDemo {public static void main(String[] args
) {System.out
.println("hello1");System.out
.println("hello2");System.out
.println("hello3");System.out
.println("hello4");System.out
.println("hello5");System.out
.println("hello6");System.out
.println("hello7");System.out
.println("hello8");}
}
選擇結構(重要)
- if單選擇結構
- if雙選擇結構
- if多選擇結構
- 嵌套的if結構
- switch多選擇結構
if單選擇結構
- 判斷一個東西是否可行,然后去執行,過程if來表示
- 語法:
if(布爾表達式
){
}
package com.boss.struct;import java.util.Scanner;public class ifDemo01
{public static void main(String[] args
) {Scanner scanner
=new Scanner(System.in
);System.out
.println("請輸入內容");String a
= scanner
.nextLine();if(a
.equals("hello")){System.out
.println(a
);}System.out
.println("End");scanner
.close();}
}
if雙選擇結構
if(布爾表達式
){
}else{
}
package com.boss.struct;import java.util.Scanner;public class ifDemo02
{public static void main(String[] args
) {Scanner scanner
=new Scanner(System.in
);System.out
.println("輸入內容:");int score
=scanner
.nextInt();if(score
>=60){System.out
.println("成績及格");}else {System.out
.println("成績不及格");}scanner
.close();}
}
if多選擇結構
if(布爾表達式
1){
}else if(布爾表達式
2){
}else if(布爾表達式
3){
}else{
}
package com.boss.struct;import java.util.Scanner;public class IFDemo03 {public static void main(String[] args
) {Scanner scanner
=new Scanner(System.in
);System.out
.println("輸入內容:");int score
=scanner
.nextInt();if(score
==100){System.out
.println("滿分");}else if (score
>=90){System.out
.println("優秀");}else if(score
>=80){System.out
.println("良好");}else if(score
>=60){System.out
.println("及格");}else {System.out
.println("不及格");}scanner
.close();}
}
package com.boss.struct;import java.util.Scanner;public class IFDemo03 {public static void main(String[] args
) {Scanner scanner
=new Scanner(System.in
);System.out
.println("輸入內容:");double score
=scanner
.nextDouble();if(score
==100){System.out
.println("滿分");}else if (score
<100&& score>=90){System.out
.println("優秀");}else if(score
<80&& score>=70 ){System.out
.println("良好");}else if(score
<70&& score>=60){System.out
.println("及格");}else if(score
<60&& score
==0){System.out
.println("不及格");}else {System.out
.println("成績不合法");}scanner
.close();}
}
嵌套if語句
- 使用嵌套if……else是合法的,可以在一個if里面嵌套一個if
- 語法
if(布爾表達式
1){if(布爾表達式
2){
}
}
switch多選擇結構
- 多選擇還有一個實現就是 switch case語句
- switch case語句判斷一個變量與一個系列某個值是否相等,每個值稱為一個分支。
- switch語句的變量類型可以是
- byte,short,int或者char
- 從java SE 7開始 switch支持字符串string類型了
- 同時case標簽必須為字符串常量或字面量
- 語法
switch(expression
){case value
:break;case value
:break;default:
}
用法
package com.boss.struct;public class SwitchDemo01 {public static void main(String[] args
) {char grade
='D';switch (grade
){case 'A':System.out
.println("優秀");break;case 'B':System.out
.println("良好");break;case 'C':System.out
.println("及格");break;case 'D':System.out
.println("不及格");break;case 'E':System.out
.println("掛科");break;default:System.out
.println("未知");}}
}
switch支持字符串string類型了
package com.boss.struct;public class SwitchDemo02 {public static void main(String[] args
) {String name
="開開心心";switch (name
){case "看看":System.out
.println("看看");break;case "開開心心":System.out
.println("開開心心");break;default:System.out
.println("干嘛呢");}}
}
反編譯,看源碼
總結
以上是生活随笔為你收集整理的顺序结构,选择结构,反编译的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。