-2

当我传递某个浮点值以与我的条件语句进行比较时,它总是执行 else 部分。

有一些带值的变量:

float total = 74.67 ; 
String grade = "", point = "";

有了这个值,我想这样做:

if(total>=80){
        grade = "A+";
        point = "4.00";
}else if(total<=79 && total>=75){
        grade = "A";
        point = "3.75";
}else if(total>=70 && total<=74) {
        grade = "A-";
        point = "3.50";
}else if(total<=65 && total>=69){
        grade = "B+";
        point = "3.25";
}else if(total<=64 && total>=60){
        grade = "B";
        point = "3.00";
}else if (total<=59 && total>=55){
        grade = "B-";
        point = "2.75";
}else if (total<=54 && total>=50){
        grade = "C+";
        point = "2.50";
}else if(total<=49 && total>=45){
        grade = "C";
        point = "2.25";
}else if (total<=44 && total>=40){
        grade = "D";
        point = "2.00";
}else {
        grade = "F";
        point = "0.00";
}

但它总是显示等级=“F”和点=“0.00”。

如果我写这个,

if(total>=70 && total<=74) {
        grade = "A-";
        point = "3.50";
    }

它显示等级=""和点=""。

总的值显示得很好,但是成绩和分数有问题。谁能告诉我有什么问题?

4

3 回答 3

1

您在 79-75 和 70-74 之间进行检查,但在 74-75 之间缺少部分

...
else if(total<=79 && total>=75){ 
        grade = "A";
        point = "3.75";
    }else if(total>=70 && total<=74) {
        grade = "A-";
        point = "3.50";
    }
...

将 更改java total>=70 && total<=74java total>=70 && total<75包括从 70-小于 75

所以你的最终代码将是

if(total>=80){
        grade = "A+";
        point = "4.00";
    }else if(total<80 && total>=75){
        grade = "A";
        point = "3.75";
    }else if(total>=70 && total<75) {
        grade = "A-";
        point = "3.50";
    }else if(total>=65 && total<70){ // here was another mistake
        grade = "B+";
        point = "3.25";
    }else if(total<65 && total>=60){
        grade = "B";
        point = "3.00";
    }else if (total<60 && total>=55){
        grade = "B-";
        point = "2.75";
    }else if (total<55 && total>=50){
        grade = "C+";
        point = "2.50";
    }else if(total<50 && total>=45){
        grade = "C";
        point = "2.25";
    }else if (total<45 && total>=40){
        grade = "D";
        point = "2.00";
    }
    else {
        grade = "F";
        point = "0.00";
    }
于 2019-12-11T05:05:49.723 回答
0

我认为你应该尝试

如果您想在“A 级”中打印此值,请尝试

if(total<80 && total>74){
    grade = "A";
    point = "3.75";
}else if(total>=70 && total<=74) {
    grade = "A-";
    point = "3.50";
}

或者

如果你想在“A级-”中打印这个值然后试试

 if(total<80 && total>=75){
    grade = "A";
    point = "3.75";
}else if(total>=70 && total<75) {
    grade = "A-";
    point = "3.50";
}
于 2019-12-11T05:08:32.870 回答
0

这里没有一个条件得到满足,这就是它执行 else 部分的原因。

但这不仅会发生在 74.67 上,还会发生在许多其他值上,你的情况应该是这样的,

if(total>81){
        grade = "A+";
        point = "4.00";
    }else if(total<80 && total>76){
        grade = "A";
        point = "3.75";
    }else if(total>71 && total<75) {
        grade = "A-";
        point = "3.50";
    }else if(total>66 && total<70){ // here was another mistake
        grade = "B+";
        point = "3.25";
    }else if(total<65 && total>61){
        grade = "B";
        point = "3.00";
    }else if (total<60 && total>56){
        grade = "B-";
        point = "2.75";
    }else if (total<55 && total>51){
        grade = "C+";
        point = "2.50";
    }else if(total<50 && total>46){
        grade = "C";
        point = "2.25";
    }else if (total<45 && total>41){
        grade = "D";
        point = "2.00";
    } else {
        grade = "F";
        point = "0.00";
    }

在这里而不是 if else 你应该使用 switch case,它会更优化并且需要更少的编译时间

于 2019-12-11T05:34:38.667 回答