选择结构讲解
1.if單、雙、多選擇結構
語法:
單:
if(布爾表達式){
}
雙:
if(布爾表達式){
}else{
}
2.嵌套的if結構
3.switch多選擇結構
example:
package com.wuming.struct;
import java.util.Scanner;
public class IfDemo01 {
? ? public static void main(String[] args) {
? ? ?? ? //if單選擇
? ? ? ? Scanner scanner = new Scanner(System.in);
? ? ? ? System.out.println("請輸入內容:");
? ? ? ? String s = scanner.nextLine();
? ? ? ? //equals判斷字符串是否相等
? ? ? ? if (s.equals("hello")){
? ? ? ? ? ? System.out.println(s);
? ? ? ? }
? ? ? ? System.out.println("End");
? ? ? ? scanner.close();
? ? }
}
請輸入內容:
s
End
請輸入內容:
hello
hello
End
package com.wuming.struct;
import java.util.Scanner;
public class IfDemo02 {
? ? public static void main(String[] args) {
? ? //if雙選擇
? ? ? ? //考試分數大于60就是及格,小于60不及格
? ? ? ? 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();
? ? }
}
請輸入成績:
60
不及格
總結
- 上一篇: 包机制、阿里巴巴开发手册
- 下一篇: BugkuCTF-WEB题源码