Java開發入門之第一階段(九)——流程之判斷語句練習(二)
阿新 • • 發佈:2021-01-05
語句練習
練習1
指定考試成績,判斷成績的等級:
- 90 - 100 優秀
- 80 - 90 好
- 70-80 良好
- 70 - 60 及格
- 60以下 不合格
public class if_else_if練習 {
public static void main(String[] args) {
int score = 100;
if (score<0 || score >100) {
System.out.println("你的成績是錯誤的!");
}else if(score>90 && score <= 100) {
System.out.println("成績優秀!");
}else if (score>80 && score<=90) {
System.out.println("成績好");
}else if (score >70 && score <= 80) {
System.out.println("成績良好!");
}else if (score >=60 && score <=70) {
System.out.println ("成績及格!");
}else {
System.out.println("不及格!!!!!");
}
}
}
執行結果如下:
練習2
題目:
使用三元運算子和標準的if-else語句分別實現:取兩個數字當中最大值
1.使用三元運算子:
public class test2 {
public static void main(String[] args) {
int a = 10;
int b = 20;
//首先使用三元運算子
int max = a > b ? a : b;
System.out. println("最大值" + max);
}
}
結果如下:
2.使用if-else語句:
int a = 10;
int b = 20;
int max;
if (a > b) {
max = a;
}else {
max = b;
}
System.out.println("最大值" + max);
結果和上面的是一樣的