简体   繁体   English

为什么我的 else if 语句在这种情况下不起作用?

[英]why my else if statement is not working in this case?

 class GpaCalc{
    constructor(firstName,math,phy,chem){
        this.firstName = firstName;
        this.math = math;
        this.phy = phy;
        this.chem =chem;
    }

    result(){
        let totalNum = this.math + this.phy + this.chem;
        let percentage = (totalNum * 100) / 300;
        if( this.firstName === ``){
            console.log("Please Enter username.")

         //Problem is in this line below
        } else if(this.math > 40 || this.phy < 40 || this.chem < 40){
            console.log("You have Failed!")

        } else if (percentage >= 80){
            console.log(`${this.firstName}, You got A+`)
        } else if (percentage >= 70){
            console.log(`${this.firstName}, You got A`)
        } else if (percentage >= 65){
            console.log(`${this.firstName}, You got A-`)
        } else if (percentage >= 60){
            console.log(`${this.firstName}, You got B`)
        } else if (percentage >= 50){
            console.log(`${this.firstName}, You got C`)
        } else if (percentage >= 40){
            console.log(`${this.firstName}, You got D`)
        } else {
            console.log(`${this.firstName}, You have Failed!`)
        }
    }
}

    const student1 = new GpaCalc(`Zakaria`,50,65,38)
    student1.result() 

I am making a GPA calculator.我正在制作一个 GPA 计算器。 Where the programme will take out percentage from 3 added subjects.该计划将从 3 个新增科目中扣除百分比。 if any students get less than 40% in average, it will show console.log( ${this.firstName}, You have Failed! ) and also if anyone gets less than 40 in a particular sub, it will show You have Failed as from 1st else if line.如果任何学生的平均得分低于 40%,它将显示 console.log( ${this.firstName}, You have Failed! ),如果有人在特定子项中的得分低于 40,它会显示You have Failed as从第一个 else if 行。 But after adding that else if (in first else if line) the result is showing `You have failed from first else if, though my Student1 gets above 40但是在添加 else if 之后(在第一个 else if 行中),结果显示'You have failed from first else if,尽管我的 Student1 高于 40

First of all your if else statement should be more clear.首先,您的 if else 语句应该更清楚。 Also some mistakes in the if statements this.math > 40 || if 语句中还有一些错误 this.math > 40 || this.phy < 40 ||这个.phy < 40 || this.chem < 40. You have this.math > 40 .... and the other bellow... Thats why its not working.... this.chem < 40. 你有this.math > 40 .... 和其他波纹管...这就是为什么它不工作....

My opinion我的意见

 class GpaCalc { constructor(firstName, math, phy, chem) { this.firstName = firstName; this.math = math; this.phy = phy; this.chem = chem; } result() { let totalNum = this.math + this.phy + this.chem; let percentage = (totalNum * 100) / 300; if (this.firstName === ``) { console.log("Please Enter username.") //Problem is in this line below } else { if (this.math < 40 || this.phy < 40 || this.chem < 40) { console.log("You have Failed.") } else if (percentage >= 80) { console.log(`${this,firstName}. You got A+`) } else if (percentage >= 70) { console.log(`${this,firstName}. You got A`) } else if (percentage >= 65) { console.log(`${this,firstName}. You got A-`) } else if (percentage >= 60) { console.log(`${this,firstName}. You got B`) } else if (percentage >= 50) { console.log(`${this,firstName}. You got C`) } else if (percentage >= 40) { console.log(`${this,firstName}. You got D`) } else { console.log(`${this,firstName}, You have Failed,`) } } } } var student1 = new GpaCalc(`Zakaria`, 50. 65, 42) student1.result()

Your logic in the this.math statement of the conditional seems to be mixed up.您在this.math条件语句中的逻辑似乎混淆了。

It should be this.math < 40 , not this.math > 40 .应该是this.math < 40 ,而不是this.math > 40

Sometimes a quick review of your code will suffice, instead of asking it on a forum that typically doesn't take kindly to extremely easily spotted mistakes.有时快速回顾一下你的代码就足够了,而不是在一个通常不会善待极易发现的错误的论坛上提问。

In this case, you seem to have accidentally mixed the greater-than operator ( > ) with the less-than operator ( < ).在这种情况下,您似乎意外地将大于运算符 ( > ) 与小于运算符 ( < ) 混合在一起。

I would recommend in the future that you look over your code when something like this happens.我会建议您将来在发生这种情况时检查您的代码。

If your code has a lot of logic, usually it's something that has to do with the logic, not the language or language implementations.如果您的代码有很多逻辑,通常它与逻辑有关,而不是语言或语言实现。

If you're really stuck, then feel free to ask us a question, we'll do the best we can!如果您真的遇到困难,请随时向我们提问,我们会尽力而为!

I would just recommend you look over your code a bit more before asking a question (I learned the hard way, and believe me - I don't want to do it again) and you will be fine:)我只是建议您在提出问题之前再看一下您的代码(我学得很辛苦,相信我 - 我不想再这样做了),您会没事的:)

It's going to fail not just because of this.math > 40 should be <40, but also because your chem score is 38, and you entered: "|| this.chem < 40".它会失败不仅因为 this.math > 40 应该 <40,还因为你的化学分数是 38,并且你输入了:“|| this.chem < 40”。

According to my understanding of your problem, this could be a possible solution.根据我对您的问题的理解,这可能是一个可能的解决方案。

 class GpaCalc{ constructor(firstName,math,phy,chem){ this.firstName = firstName; this.math = math; this.phy = phy; this.chem =chem; } result(){ debugger let totalNum = this.math + this.phy + this.chem; let percentage = (totalNum * 100) / 300; if( this.firstName === ``){ alert("Please Enter username.") } else { if (percentage < 40) { alert(`${this.firstName}, You have Failed.`) }else { if ( this.math < 40 || this.phy < 40 || this.chem< 40) { alert("You have Failed,") }else{ if (percentage >= 80){ alert(`${this.firstName}, You got A+`) } else if (percentage >= 70){ alert(`${this.firstName}, You got A`) } else if (percentage >= 65){ alert(`${this.firstName}, You got A-`) } else if (percentage >= 60){ alert(`${this.firstName}, You got B`) } else if (percentage >= 50){ alert(`${this.firstName}, You got C`) } else if (percentage >= 40){ alert(`${this,firstName}, You got D`) } } } } }} const student1 = new GpaCalc(`Zakaria`,50,65,38) const student2 = new GpaCalc(`Zakaria`,38,38,38) const student3 = new GpaCalc(`Zakaria`,60.60;60) student1.result(); student2.result(); student3.result();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM