11.java流程控制01
阿新 • • 發佈:2021-01-25
技術標籤:java
順序結構
public class ShunXunDemo {
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");
}
}
if
public class IfDemo01 {
public static void main(String[] args) {
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();
}
}
public class IfDemo02 {
public static void main(String[] args) {
//考試分數大於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();
}
}
public class IfDemo03 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
/*
if 語句至多有1個else語句,else語句在所有的else if語句之後
if 語句可以有若干個else if語句,他們必須在else語句之前
一旦其中一個else if語句檢測為true,其他的else if以及else語句都將跳過執行
*/
System.out.println("請輸入成績:");
int score = scanner.nextInt();
if (score == 100){
System.out.println("恭喜滿分!");
} else if (score < 100 && score >= 90){
System.out.println("A級");
} else if (score < 90 && score >= 80){
System.out.println("B級");
} else if (score < 80 && score >= 70){
System.out.println("C級");
} else if (score < 70 && score >= 60){
System.out.println("D級");
} else if (score < 60 && score >= 0){
System.out.println("不及格");
} else{
System.out.println("成績不合法!");
}
scanner.close();
}
}
switch
public class SwitchDemo01 {
public static void main(String[] args) {
//case穿透: case語句沒有break會一直往下執行
char grade = 'C';
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("未知等級");
}
}
}
public class SwitchDemo02 {
public static void main(String[] args) {
String name = "狂神";
/*
JDK7的新特性,表示式結果可以是字串!
字元的本質還是數字
反編譯 java---class(位元組碼檔案)---反編譯(IDEA)
*/
switch (name){
case "秦將":
System.out.println("秦將");
break;
case "狂神":
System.out.println("狂神");
break;
default:
System.out.println("弄啥嘞!");
}
}
}
/*
1. 點選File->Project Structure
2. 在Project compiler output中找到反編譯檔案路徑,開啟
3. 依次點開資料夾名,找到與本檔案同名的class檔案,拖到IDEA開啟原始碼
*/
原始碼
package com.yy.struct;
public class SwitchDemo02 {
public SwitchDemo02() {
}
public static void main(String[] args) {
String name = "狂神";
byte var3 = -1;
switch(name.hashCode()) {
case 941788:
if (name.equals("狂神")) {
var3 = 1;
}
break;
case 990944:
if (name.equals("秦將")) {
var3 = 0;
}
}
switch(var3) {
case 0:
System.out.println("秦將");
break;
case 1:
System.out.println("狂神");
break;
default:
System.out.println("弄啥嘞!");
}
}
}