if-else
if (Boolean-expression) {
// 要執行的語句
}
這是if
判斷的基本形式,和C語言沒有什麼差別,括號中的布爾表達式(Boolean-expression)必須要生成一個boolean
類型的結果,要麼是true
要麼是false
,而花括號中的語句塊只會在表達式爲true
時執行。
和C語言一樣,當if
後面只有一個分號;
結尾的語句時,可以省略花括號,但是爲了以後修改程序方便,建議任何時候都保持有花括號的統一格式。
public class IfElse {
public static void main(String[] args) {
int n = 95;
if (n >= 60) {
System.out.println("及格");
} else {
System.out.println("掛科");
}
}
}
public class IfElse {
public static void main(String[] args) {
int n = 95;
if (n >= 90) {
System.out.println("優秀");
} else if (n >= 60) {
System.out.println("合格");
} else {
System.out.println("掛科");
}
}
}
上面兩個程序是if-else
結構,和C語言一樣,else if
只是else後面跟了一個if語句,不像Python中的elif
是個專門的關鍵字。
判斷字符串是否相等
String
字符串是引用類型,在上一篇中講到了引用類型的概念,引用類型的變量名實質上是指向對象在堆內存中的地址。考慮下面這個例子:
public class Main {
public static void main(String[] args) {
String s1 = "hello";
String s2 = "hello";
System.out.println(s1);
System.out.println(s2);
if (s1 == s2) {
System.out.println("s1 == s2");
} else {
System.out.println("s1 != s2");
}
}
}
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
這個值,卻是放在不同內存空間裏的兩個字符串對象。
爲了精確判斷兩個字符串的值是否相等,應該使用equals()
方法。
String s1 = "hello";
String s2 = "Hello";
if (s1.equals(s2)) {
System.out.println("相等");
} else {
System.out.println("不相等");
}
switch
public class Main {
public static void main(String[] args) {
int option = 2;
switch (option) {
case 1:
System.out.println("Selected 1");
break;
case 2:
case 3:
System.out.println("Selected 2, 3");
break;
default:
System.out.println("Not selected");
break;
}
}
}
switch可以用if來代替。要注意每個case
後面有冒號,而不需要花括號,每個case
需要有break
,否則將繼續將後面的case
內容執行,當然有些情況就是不同的case
要執行的操作相同,如上面例子中的2和3。default
是所有case
都不匹配時執行的。switch
還可以選擇字符串,比較的是字符串的值。
Python中並沒有switch語句,設計者認爲switch會破壞Python的靈活性。
while循環
while (Boolean-expression) {
// 要執行的語句
}
和C語言沒有區別,只要滿足條件,就會一直執行while
中的語句,同時Java中也有do-while
循環,do-while
相比while
的區別是保證循環中的語句至少要被執行一次。
do {
// 要執行的語句
} while (Boolean-expression)
for循環與for-in循環
for (初始條件; 循環檢測條件; 循環後更新計數器) {
// 執行語句
}
和C語言的for
循環沒有什麼區別,但在Java中,還實現了一種更加便捷的for-in
循環:
int[] s = { 1, 2, 3, 4, 5, 6 };
for (int i = 0; i < s.length, i++) {
System.out.println(s[i]);
}
這樣遍歷一個數組比較麻煩,用for-in
的方法來寫是這樣:
int[] s = { 1, 2, 3, 4, 5, 6 };
for (int n: s) {
System.out.println(n);
}
for-in
不使用索引,直接去遍歷一個序列的每個元素,寫起來更爲簡潔。
吐槽一下,相比起Python中for i in arr
和C#中的foreach (類型 變量名 in 序列)
的寫法比起來,這種for (類型 變量名: 序列)
的奇葩寫法讓人一眼根本看不出來是在做什麼啊。
順帶一提:
for (int i = 0; i < 10 ; i++) {
}
這裏這個變量i
的作用域僅僅在for循環體內部,出了循環就無法使用i了,如果希望循環結束還可以訪問i,可以這樣寫:
int i;
for (i = 0; i < 10; i++) {
}
break和continue
public class Main {
public static void main(String[] args) {
int i;
for (i = 0; i < 100; i++) {
if (i == 97) {
break; // 跳出循環
}
if (i % 2 == 0) {
continue; // 下一次循環
}
System.out.println("在沒有continue時會執行" + i);
}
System.out.println("跳出循環後執行" + i);
}
}
break
直接跳出當前循環,強調當前是因爲有時候會使用嵌套循環,一個break
只會跳出所在的那一層循環。continue
則是直接本次循環結束,進入下一次循環。