第三章 選擇語句
3.1 普通
java
if
、else if
、else
package 選擇語句;
public class dome {
public static void main(String[] args) {
sunxv();
tiaojian();
}
public static void sunxv(){
System.out.println("初次見面請多關照");
System.out.println("我是循序結構");
}
public static void tiaojian() {
int a = 100;
if(a>=500) {
System.out.println("我是條件判斷語句-------------if");
}
else if(a>=30) {
System.out.println("我是else if語句");
}
else{
System.out.println("我是條件判斷語句else");
}
}
}
python
if---else----elif
a = 100
if a >500:
print('a的值爲500')
elif a > 90:
print('a的值爲100')
else:
print('a的值未知')
3.2 switch----case----default----break
package 選擇語句;
public class stiwch_case {
public static void main(String[] args) {
switch_case();
}
public static void switch_case() {
System.out.println("這是switch語句");
int a = 100;
switch (a) {
case 1:
System.out.println("a的值爲1");
break;
case 50:
System.out.println("a的值爲50");
break;
case 100:
System.out.println("a的值爲100");
break;
default:
System.out.println("你竟然沒猜對");
break;
}
}
}
-
多個
case
後面的值不能相同 -
switch只能對
byte/short/char/int/String/enum
進行判斷 -
switch判斷語句可以顛倒,break可以省略
-
不寫break會繼續執行下面的case
3.3 循環結構
3.3.1 for循環----while循環-----do+while循環
java
package 選擇語句;
public class for_while {
public static void main(String[] args) {
for_();
while_();
dowhile();
}
public static void for_() {
System.out.println("開始for循環");
for (int a = 0; a <= 20;a ++) {
System.out.println("我錯了");
}
}
public static void while_() {
System.out.println("while循環開始");
int a = 0;
while(a <10) {
System.out.println(a);
a ++;
}
}
public static void dowhile() {
System.out.println("do while循環開始");
int a = 10;
do {
System.out.println(a);
a ++;
}while(a<100);
}
}
python
for i in range(10):
print(i)
while data:
print('data裏面是有數據的')