大家好呀,上一期基礎加固篇反向很熱烈呀,這期我就就像在代碼中帶大家回顧Java中的基礎知識,可以自己敲一下看看,加深理解,根據這些知識我還整理了一張圖,基本上每個知識點都有注釋詳解,大家有需要的可以私信找我要,
if陳述句
在Java程式中,如果要根據條件來決定是否執行某一段代碼,就需要if陳述句,
if陳述句的基本語法是:
if (條件) {
// 條件滿足時執行
}
根據if的計算結果(true還是false),JVM決定是否執行if陳述句塊(即花括號{}包含的所有陳述句),
讓我們來看一個例子:
// 條件判斷
public class Main {
public static void main(String[] args) {
int n = 70;
if (n >= 60) {
System.out.println("及格了");
}
System.out.println("END");
}
}
運行結果:
及格了
END
當條件n >= 60計算結果為true時,if陳述句塊被執行,將列印"及格了",否則,if陳述句塊將被跳過,修改n的值可以看到執行效果,
if…else if
可以用多個if … else if …串聯,例如:
// 條件判斷
public class Main {
public static void main(String[] args) {
int n = 70;
if (n >= 90) {
System.out.println("優秀");
} else if (n >= 60) {
System.out.println("及格了");
} else {
System.out.println("掛科了");
}
System.out.println("END");
}
}
運行結果:
及格了
END
判斷參考型別相等:equals( )方法
在Java中,判斷值型別的變數是否相等,可以使用==運算子,但是,判斷參考型別的變數是否相等,==表示“參考是否相等”,或者說,是否指向同一個物件,例如,下面的兩個String型別,它們的內容是相同的,但是,分別指向不同的物件,用==判斷,結果為false:
// 條件判斷
public class Main {
public static void main(String[] args) {
String s1 = "hello";
String s2 = "HELLO".toLowerCase();
System.out.println(s1);
System.out.println(s2);
if (s1 == s2) {
System.out.println("s1 == s2");
} else {
System.out.println("s1 != s2");
}
}
}
運行結果:
hello
hello
s1 != s2
要判斷參考型別的變數內容是否相等,必須使用equals()方法:
// 條件判斷
public class Main {
public static void main(String[] args) {
String s1 = "hello";
String s2 = "HELLO".toLowerCase();
System.out.println(s1);
System.out.println(s2);
if (s1.equals(s2)) {
System.out.println("s1 equals s2");
} else {
System.out.println("s1 not equals s2");
}
}
}
運行結果:
hello
hello
s1 equals s2
注意:執行陳述句s1.equals(s2)時,如果變數s1為null,會報NullPointerException:
// 條件判斷
public class Main {
public static void main(String[] args) {
String s1 = null;
if (s1.equals("hello")) {
System.out.println("hello");
}
}
}
運行結果:
Exception in thread “main” java.lang.NullPointerException
at Main.main(Main.java:5)
要避免NullPointerException錯誤,可以利用短路運算子&&:
// 條件判斷
public class Main {
public static void main(String[] args) {
String s1 = null;
if (s1 != null && s1.equals("hello")) {
System.out.println("hello");
}
}
}
還可以把一定不是null的物件"hello"放到前面:例如:if ("hello".equals(s)) { ... },
switch 多重選擇
除了if陳述句外,還有一種條件判斷,是根據某個運算式的結果,分別去執行不同的分支,
例如,在游戲中,讓用戶選擇選項:
單人模式
多人模式
退出游戲
這時,switch陳述句就派上用場了,
switch陳述句根據switch (運算式)計算的結果,跳轉到匹配的case結果,然后繼續執行后續陳述句,直到遇到break結束執行,
我們看一個例子:
// switch
public class Main {
public static void main(String[] args) {
int option = 1;
switch (option) {
case 1:
System.out.println("Selected 1");
break;
case 2:
System.out.println("Selected 2");
break;
case 3:
System.out.println("Selected 3");
break;
}
}
}
運行結果:
Selected 1
如果option的值沒有匹配到任何case,例如option = 99,那么,switch陳述句不會執行任何陳述句,這時,可以給switch陳述句加一個default,當沒有匹配到任何case時,執行default:
// switch
public class Main {
public static void main(String[] args) {
int option = 99;
switch (option) {
case 1:
System.out.println("Selected 1");
break;
case 2:
System.out.println("Selected 2");
break;
case 3:
System.out.println("Selected 3");
break;
default:
System.out.println("Not selected");
break;
}
}
}
運行結果:
Not selected
如果把switch陳述句翻譯成if陳述句,那么上述的代碼相當于:
if (option == 1) {
System.out.println("Selected 1");
} else if (option == 2) {
System.out.println("Selected 2");
} else if (option == 3) {
System.out.println("Selected 3");
} else {
System.out.println("Not selected");
}
對于多個==判斷的情況,使用switch結構更加清晰,
switch陳述句還可以匹配字串,字串匹配時,是比較“內容相等”
switch陳述句還可以匹配字串,字串匹配時,是比較“內容相等”,例如:
// switch
public class Main {
public static void main(String[] args) {
String fruit = "apple";
switch (fruit) {
case "apple":
System.out.println("Selected apple");
break;
case "pear":
System.out.println("Selected pear");
break;
case "mango":
System.out.println("Selected mango");
break;
default:
System.out.println("No fruit selected");
break;
}
}
}
運行結果:
Selected apple
穿透性
使用switch時,注意case陳述句并沒有花括號{},而且,case陳述句具有“穿透性”,漏寫break將導致意想不到的結果:
// switch
public class Main {
public static void main(String[] args) {
int option = 2;
switch (option) {
case 1:
System.out.println("Selected 1");
case 2:
System.out.println("Selected 2");
case 3:
System.out.println("Selected 3");
default:
System.out.println("Not selected");
}
}
}
運行結果:
Selected 2
Selected 3
Not selected
當option = 2時,將依次輸出"Selected 2"、“Selected 3”、“Not selected”,原因是從匹配到case 2開始,后續陳述句將全部執行,直到遇到break陳述句,因此,任何時候都不要忘記寫break,
回圈
回圈陳述句就是讓計算機根據條件做回圈計算,在條件滿足時繼續回圈,條件不滿足時退出回圈,
while回圈
Java提供的while條件回圈,它的基本用法是:
while (條件運算式) {
回圈陳述句
}
// 繼續執行后續代碼
while回圈在每次回圈開始前,首先判斷條件是否成立,如果計算結果為true,就把回圈體內的陳述句執行一遍,如果計算結果為false,那就直接跳到while回圈的末尾,繼續往下執行,
我們用while回圈來累加1到100,可以這么寫:
// while
public class Main {
public static void main(String[] args) {
int sum = 0; // 累加的和,初始化為0
int n = 1;
while (n <= 100) { // 回圈條件是n <= 100
sum = sum + n; // 把n累加到sum中
n ++; // n自身加1
}
System.out.println(sum); // 5050
}
}
運行結果:
5050
do while回圈
在Java中,while回圈是先判斷回圈條件,再執行回圈,而另一種do while回圈則是先執行回圈,再判斷條件,條件滿足時繼續回圈,條件不滿足時退出,它的用法是:
do {
執行回圈陳述句
} while (條件運算式);
可見,do while回圈會至少回圈一次,
我們把對1到100的求和用do while回圈改寫一下:
// do-while
public class Main {
public static void main(String[] args) {
int sum = 0;
int n = 1;
do {
sum = sum + n;
n ++;
} while (n <= 100);
System.out.println(sum);
}
}
運行結果:
5050
使用do while回圈時,同樣要注意回圈條件的判斷,
注意到while回圈是先判斷回圈條件,再回圈,因此,有可能一次回圈都不做,
for回圈
for回圈的功能非常強大,它使用計數器實作回圈,for回圈會先初始化計數器,然后,在每次回圈前檢測回圈條件,在每次回圈后更新計數器,計數器變數通常命名為i,
我們把1到100求和用for回圈改寫一下:
// for
public class Main {
public static void main(String[] args) {
int sum = 0;
for (int i=1; i<=100; i++) {
sum = sum + i;
}
System.out.println(sum);
}
}
運行結果:
5050
在for回圈執行前,會先執行初始化陳述句int i=1,它定義了計數器變數i并賦初始值為1,然后,回圈前先檢查回圈條件i<=100,回圈后自動執行i++,因此,和while回圈相比,for回圈把更新計數器的代碼統一放到了一起,在for回圈的回圈體內部,不需要去更新變數i,
因此,for回圈的用法是:
for (初始條件; 回圈檢測條件; 回圈后更新計數器) {
// 執行陳述句
}
回圈控制陳述句
break:跳出當前回圈
continue:提前結束本次回圈,直接繼續執行下次回圈
最后,祝大家早日學有所成,拿到滿意offer,快速升職加薪,走上人生巔峰, 可以的話請給我一個三連支持一下我喲,我們下期再見

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/295632.html
標籤:java
