【问题标题】:My code is supposed to take in a number, and return either the letter grade or "Grade is not valid" but the else statement is not working我的代码应该接受一个数字,并返回字母等级或“等级无效”,但 else 语句不起作用
【发布时间】:2019-10-14 21:35:20
【问题描述】:

我的代码应该接受一个数字,并返回字母等级或“等级无效”,但 else 语句不起作用。

#include<iostream>

using namespace std;

int main(){
    int score = 0;
    string response  = "Grade is ";
    cout<<"Please enter score: "<<endl;
    cin>>score;
    if(score<100 || score>0){
        if (score>=0 && score<60)
            cout<<response<<"F"<<endl;
        if(score>=60 && score<70)
            cout<<response<<"D"<<endl;
        if(score>=70 && score<80)
            cout<<response<<"C"<<endl;
        if(score>=80 && score<90)
            cout<<response<<"B"<<endl;
        if (score>=90 && score<100)
            cout<<response<<"A"<<endl;
    }
    else
        cout<<"Not a valid score"<<endl;

}

【问题讨论】:

  • 假设分数是-1score&lt;100 || score&gt;0的结果是什么?
  • " is not working" 不是对您的问题的有效描述
  • 只需将score&lt;100 || score&gt;0 切换为score &lt;= 100 &amp;&amp; score &gt;=0 并将score&gt;=90 &amp;&amp; score&lt;100 切换为score&gt;=90 &amp;&amp; score&lt;=100

标签: c++


【解决方案1】:

你的逻辑是错误的。分数应大于或等于 0 AND 小于或等于 100,而不是 OR。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-18
    • 1970-01-01
    • 1970-01-01
    • 2019-11-13
    • 1970-01-01
    • 1970-01-01
    • 2021-10-04
    • 1970-01-01
    相关资源
    最近更新 更多