C++ c++;在我的程序中使用if、elseif和else语句?(新手)
我不明白循环if语句或者为什么我的程序不能正常工作。该程序将根据您的输入反映您的成绩。有人知道为什么它不起作用吗C++ c++;在我的程序中使用if、elseif和else语句?(新手),c++,if-statement,C++,If Statement,我不明白循环if语句或者为什么我的程序不能正常工作。该程序将根据您的输入反映您的成绩。有人知道为什么它不起作用吗 #include <iostream> using namespace std; int main() { int grade; cout << "What grade did you earn in Programming I ?" << endl; cin >> grade; if (grade =
#include <iostream>
using namespace std;
int main()
{
int grade;
cout << "What grade did you earn in Programming I ?" << endl;
cin >> grade;
if (grade = 'A')
cout << "an A - excellent work !" << endl;
if (grade = 'B')
cout << "you got a B - good job" << endl;
else if (grade = 'C')
cout << "earning a C is satisfactory" << endl;
else if (grade = 'D')
cout << "while D is passing, there is a problem" << endl;
else if (grade = 'F')
cout << "you failed - better luck next time" << endl;
else
cout << "You did not enter an A,B,C,D or F" << endl;
return 0;
}
#包括
使用名称空间std;
int main()
{
国际等级;
cout分级;
如果(等级='A')
cout问题出现在if
条件下的=
中
=
是赋值运算符,而=
是相等比较运算符
当您为int
赋值时,它的计算结果也为true,因此也满足if条件
此外,如评论中所述,将分数的数据类型从int
更改为char
。在任何情况下将=
更改为=
也将分数的数据类型从int
更改为char
。阅读一本好的初学者书籍。或者,实际上,任何初学者书籍。此外,请不要养成调用if语句循环。它们是条件语句,与循环没有任何关系。并且您需要在“B
”分支前面使用else if
,而不仅仅是if
。