【问题标题】:Error when trying to add another clause into code尝试将另一个子句添加到代码中时出错
【发布时间】:2018-10-13 14:50:13
【问题描述】:

我是编码新手,我正在处理一项任务。我创建了一个代码来输入测试分数并根据分数读出成绩。我正在尝试添加一个子句,该子句将为高于 100 且低于 0 的分数显示“无效”,但我找不到添加它的方法,而不会在代码中收到某种错误。我尝试添加另一个 if 子句并且它有效,但它打印出无效的分数,这确实是有效的。

#include <iostream>
using namespace std;
int main()
{
     int testScore;

    cout << "Enter test score" << endl;
    cin >> testScore;

    if (testScore >= 90)
        cout << "Your grade is A" << endl;
    else if (testScore >= 80)
        cout << "Your grade is B" << endl;
    else if (testScore >= 70)
        cout << "Your grade is C" << endl;
    else if (testScore >= 60)
        cout << "Your grade is D" << endl;
    else 
        cout << "Your grade is F" << endl;

    return 0;
}

【问题讨论】:

标签: c++


【解决方案1】:

你想要

if (testScore &gt; 100 || testScore &lt; 0)

在 if-else-if 链的开头。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-17
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 2017-05-04
    • 2018-01-06
    相关资源
    最近更新 更多