gpt4 book ai didi

c++ - 尝试将另一个子句添加到代码中时出错

转载 作者:行者123 更新时间:2023-11-27 23:43:09 26 4
gpt4 key购买 nike

我是编码新手,正在完成一项作业。我创建了一个代码来输入考试分数并根据分数读出成绩。我正在尝试添加一个子句,该子句将对高于 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;
}

最佳答案

你想要

if (testScore > 100 || testScore < 0)

在您的 if-else-if 链的开头。

关于c++ - 尝试将另一个子句添加到代码中时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52794163/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com