练习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);
结果和上面的是一样的