day03-if-三种格式的学习与练习

This commit is contained in:
2025-12-24 14:23:36 +08:00
parent e18a14b025
commit c17d725183

View File

@@ -51,12 +51,42 @@ else{
*/
public class Demo01_if {
//if的格式三的练习
public static void main(String[] args) {
int score =77;
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("你的成绩属于不及格");
}
}
//if的格式三
public static void ifMethod3(String[] args) {
// x和y的关系满足如下
// x>=3 y = 2x + 1;
//-1<=x<3 y = 2x;
// x<=-1 y = 2x 1;
// x<-1 y = 2x 1;
// 根据给定的x的值计算出y的值并输出。
int x = -2;
int y;
if (x >= 3) {
y = 2*x + 1;
} else if (x>=-1 && x<3) {
y = 2*x;
} else {
y = 2*x-1;
}
System.out.println(y);
}