只返回第一个 if 语句? (C++)
Only Returning the first if statement? (C++)
这是我正在处理的作业:
Write a program that prompts the user to enter the points earned for a course and determines the grade. The program should display the grade and the message string for the grade. Use 'if else' statements to determine the grade and the switch statement to print the message string.
我编写了代码并且它执行时没有任何调试错误。问题是,无论我输入 PointsEarned
什么,我都只得到 F 的输出!例如,如果我输入“90”:
Try Harder Next Time.
GRADE: F
有什么问题吗?我是否将 switch 语句放在了错误的位置?这是代码:
#include <iostream>
using namespace std;
int main()
{
int PointsEarned;
char Grade;
cout << "Please input the points earned in the course: ";
cin >> PointsEarned;
if (0 <= PointsEarned < 60) {
Grade = 'F';
}
else if (60 <= PointsEarned < 70) {
Grade = 'D';
}
else if (70 <= PointsEarned < 80) {
Grade = 'C';
}
else if (80 <= PointsEarned < 90) {
Grade = 'B';
}
else if (90 <= PointsEarned) {
Grade = 'A';
}
else{
cout << "That is not a valid input.";
}
switch (Grade)
{
case 'F':
case 'D':
cout << "Try Harder Next Time." << endl;
break;
case 'C':
cout << "Good." << endl;
break;
case 'B':
cout << "Very good." << endl;
break;
case 'A':
cout << "Excellent." << endl;
break;
default:
cout << "Please choose a valid input to receive your grade." << endl;
}
cout << "GRADE: " << Grade << endl;
}
0 <= PointsEarned < 60
形式的比较适用于 Python 但不适用于 C++。你应该使用 0 <= PointsEarned && PointsEarned < 60
.
您不能像现在这样测试多个值:
if (0 <= PointsEarned < 60) {
这不是检查 PointsEarned
是否在 0
和 60
之间。一次只会对 <
中的 ONE 进行评估,这意味着您正在有效地测试更像
的东西
if (0 <= true/false) {
相反。
你需要更像
if ((0 <= PointsEarned) && (PointsEarned < 60)) {
相反。
这个if (0 <= PointsEarned < 60) {
正在破坏 C++ 语言,将其更改为 2 个有效比较
if (0 <= PointsEarned && PointsEarned < 60) {
我重写了你的代码并做了很少的 improvements:in else if (90 <= PointsEarned)
我将它限制为 100.Also 其他评论者指出你不能测试多个值所以我重写因为他们 said.It 现在工作正常!
#include <iostream>
using namespace std;
int main()
{
int PointsEarned;
char Grade;
cout << "Please input the points earned in the course: ";
cin >> PointsEarned;
if ((0 <= PointsEarned) && (PointsEarned < 60)) {
Grade = 'F';
}
else if ((60 <= PointsEarned) && (PointsEarned < 70)){
Grade = 'D';
}
else if ((70 <= PointsEarned) && (PointsEarned < 80)) {
Grade = 'C';
}
else if ((80 <= PointsEarned) && (PointsEarned < 90)){
Grade = 'B';
}
else if ((90 <= PointsEarned) && (PointsEarned <= 100)) {
Grade = 'A';
}
else{
cout << "That is not a valid input.";
}
switch (Grade)
{
case 'F':
case 'D':
cout << "Try Harder Next Time." << endl;
break;
case 'C':
cout << "Good." << endl;
break;
case 'B':
cout << "Very good." << endl;
break;
case 'A':
cout << "Excellent." << endl;
break;
default:
cout << "Please choose a valid input to receive your grade." << endl;
}
cout << "GRADE: " << Grade << endl;
}
表达式:if (0 <= PointsEarned < 60)
表示if ((0 <= PointsEarned) < 60)
表示if ((true/false) < 60)
以上表达式与您可能想要的表达式不同:
if ((0 <= PointsEarned) && (PointsEarned < 60))
这是我正在处理的作业:
Write a program that prompts the user to enter the points earned for a course and determines the grade. The program should display the grade and the message string for the grade. Use 'if else' statements to determine the grade and the switch statement to print the message string.
我编写了代码并且它执行时没有任何调试错误。问题是,无论我输入 PointsEarned
什么,我都只得到 F 的输出!例如,如果我输入“90”:
Try Harder Next Time.
GRADE: F
有什么问题吗?我是否将 switch 语句放在了错误的位置?这是代码:
#include <iostream>
using namespace std;
int main()
{
int PointsEarned;
char Grade;
cout << "Please input the points earned in the course: ";
cin >> PointsEarned;
if (0 <= PointsEarned < 60) {
Grade = 'F';
}
else if (60 <= PointsEarned < 70) {
Grade = 'D';
}
else if (70 <= PointsEarned < 80) {
Grade = 'C';
}
else if (80 <= PointsEarned < 90) {
Grade = 'B';
}
else if (90 <= PointsEarned) {
Grade = 'A';
}
else{
cout << "That is not a valid input.";
}
switch (Grade)
{
case 'F':
case 'D':
cout << "Try Harder Next Time." << endl;
break;
case 'C':
cout << "Good." << endl;
break;
case 'B':
cout << "Very good." << endl;
break;
case 'A':
cout << "Excellent." << endl;
break;
default:
cout << "Please choose a valid input to receive your grade." << endl;
}
cout << "GRADE: " << Grade << endl;
}
0 <= PointsEarned < 60
形式的比较适用于 Python 但不适用于 C++。你应该使用 0 <= PointsEarned && PointsEarned < 60
.
您不能像现在这样测试多个值:
if (0 <= PointsEarned < 60) {
这不是检查 PointsEarned
是否在 0
和 60
之间。一次只会对 <
中的 ONE 进行评估,这意味着您正在有效地测试更像
if (0 <= true/false) {
相反。
你需要更像
if ((0 <= PointsEarned) && (PointsEarned < 60)) {
相反。
这个if (0 <= PointsEarned < 60) {
正在破坏 C++ 语言,将其更改为 2 个有效比较
if (0 <= PointsEarned && PointsEarned < 60) {
我重写了你的代码并做了很少的 improvements:in else if (90 <= PointsEarned)
我将它限制为 100.Also 其他评论者指出你不能测试多个值所以我重写因为他们 said.It 现在工作正常!
#include <iostream>
using namespace std;
int main()
{
int PointsEarned;
char Grade;
cout << "Please input the points earned in the course: ";
cin >> PointsEarned;
if ((0 <= PointsEarned) && (PointsEarned < 60)) {
Grade = 'F';
}
else if ((60 <= PointsEarned) && (PointsEarned < 70)){
Grade = 'D';
}
else if ((70 <= PointsEarned) && (PointsEarned < 80)) {
Grade = 'C';
}
else if ((80 <= PointsEarned) && (PointsEarned < 90)){
Grade = 'B';
}
else if ((90 <= PointsEarned) && (PointsEarned <= 100)) {
Grade = 'A';
}
else{
cout << "That is not a valid input.";
}
switch (Grade)
{
case 'F':
case 'D':
cout << "Try Harder Next Time." << endl;
break;
case 'C':
cout << "Good." << endl;
break;
case 'B':
cout << "Very good." << endl;
break;
case 'A':
cout << "Excellent." << endl;
break;
default:
cout << "Please choose a valid input to receive your grade." << endl;
}
cout << "GRADE: " << Grade << endl;
}
表达式:if (0 <= PointsEarned < 60)
表示if ((0 <= PointsEarned) < 60)
表示if ((true/false) < 60)
以上表达式与您可能想要的表达式不同:
if ((0 <= PointsEarned) && (PointsEarned < 60))