我目前正在制作一个for循环,显示您的平均分数和 但我似乎无法使成绩出现。
Enter Your Name:
Quiz 1
Quiz 2
Quiz 3
Average
Grade <- this one dont appear its on the if statement in my code
using namespace std;
int main(){
int nos[5][3]; //multi-dimention
int r, c;
char name[20];
int x[3];
int av=0;
char l;
do{
system("cls");
for(r=0; r<5; r++){ //Row
cout<<"Enter Your Name : ";
cin>>name;
for(c=0; c<3; c++){ //Column
cout<<"Enter Quiz "<<"["<<r<<"]"<<"["<<c<<"] : ";
cin>>nos[r][c];
}
cout<<"Average : "<<(float)(nos[r][0]+nos[r][1]+nos[r][2])/3<<endl;
av = ((float)nos[r][0]+nos[r][1]+nos[r][2])/3;
if(av==0 && av<=74.99){
cout<<"grade = 5.0"<<endl; }
if(av ==75 && av<=76.99){
cout<<"grade = 3.0"<<endl; }
if(av ==77 && av<=79.99){
cout<<"grade = 2.75"<<endl; }
if(av ==80 && av<=82.99){
cout<<"grade = 2.50"<<endl; }
if(av ==83 && av<=84.99){
cout<<"grade = 2.25"<<endl; }
if(av ==85 && av<=87.99){
cout<<"grade = 2.0"<<endl; }
if(av ==88 && av<=90.99){
cout<<"grade = 1.75"<<endl; }
if(av ==91 && av<=93.99){
cout<<"grade = 1.50"<<endl;}
if(av ==94 && av<=96.99){
cout<<"grade = 1.25"<<endl; }
if(av ==97 && av<=100){
cout<<"grade = 1.0"<<endl;}
}
cout<<"\nTry Another [Y/N] : ";
cin>>l;
} while(l!='N' && l!='n');
getch();
}
答案 0 :(得分:4)
您if
条件错误
使用或(||
)或设置适当的下限。
例如:
if(av==0 && av<=74.99)
你应该说
if(av>=0 && av<=74.99)
或
if(av==0 || av<=74.99)
取决于你的逻辑(我猜是前者)。
另外,正如Bill正确指出的那样,您应该使用if else
而不是所有if
。即使它们适用于您的情况,if else
也是首选,因为它们可以防止出现逻辑错误。这里有更好的解释:https://stackoverflow.com/a/1796155/2302611
所以你的代码(条件部分)应该是这样的:
if(av>=0 && av<=74.99){
cout<<"grade = 5.0"<<endl;
}
else if(av >=75 && av<=76.99){
cout<<"grade = 3.0"<<endl;
}
else if(av >=77 && av<=79.99){
cout<<"grade = 2.75"<<endl;
}
else if(av >=80 && av<=82.99){
cout<<"grade = 2.50"<<endl;
}
else if(av >=83 && av<=84.99){
cout<<"grade = 2.25"<<endl;
}
else if(av >=85 && av<=87.99){
cout<<"grade = 2.0"<<endl;
}
else if(av >=88 && av<=90.99){
cout<<"grade = 1.75"<<endl;
}
else if(av >=91 && av<=93.99){
cout<<"grade = 1.50"<<endl;
}
else if(av >=94 && av<=96.99){
cout<<"grade = 1.25"<<endl;
}
else if(av >=97 && av<=100){
cout<<"grade = 1.0"<<endl;
}
else cout << "Grade not in bounds";
答案 1 :(得分:1)
你的if语句逻辑是错误,并且int不能用于存储浮点数,这里是代码更改
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
double nos[5][3]; //multi-dimention
int r, c;
char name[20];
int x[3];
double av = 0;
char l;
do{
system("cls");
for (r = 0; r<5; r++){ //Row
cout << "Enter Your Name : ";
cin >> name;
for (c = 0; c<3; c++){ //Column
cout << "Enter Quiz " << "[" << r << "]" << "[" << c << "] : ";
cin >> nos[r][c];
}
av = (nos[r][0] + nos[r][1] + nos[r][2]) / 3;
cout << "Average : " << av << endl;
if (av >= 0 && av <= 74.99){
cout << "grade = 5.0" << endl;
}
if (av >= 75 && av <= 76.99){
cout << "grade = 3.0" << endl;
}
if (av >= 77 && av <= 79.99){
cout << "grade = 2.75" << endl;
}
if (av >= 80 && av <= 82.99){
cout << "grade = 2.50" << endl;
}
if (av >= 83 && av <= 84.99){
cout << "grade = 2.25" << endl;
}
if (av >= 85 && av <= 87.99){
cout << "grade = 2.0" << endl;
}
if (av >= 88 && av <= 90.99){
cout << "grade = 1.75" << endl;
}
if (av >= 91 && av <= 93.99){
cout << "grade = 1.50" << endl;
}
if (av >= 94 && av <= 96.99){
cout << "grade = 1.25" << endl;
}
if (av >= 97 && av <= 100){
cout << "grade = 1.0" << endl;
}
}
cout << "\nTry Another [Y/N] : ";
cin >> l;
} while (l != 'N' && l != 'n');
getch();
return 0;
}
答案 2 :(得分:0)
using namespace std;
int main(){
int nos[5][3]; //multi-dimention
int r, c;
char name[20];
int x[3];
int av=0;
char l;
do{
system("cls");
for(r=0; r<5; r++){ //Row
cout<<"Enter Your Name : ";
cin>>name;
for(c=0; c<3; c++){ //Column
cout<<"Enter Quiz "<<"["<<r<<"]"<<"["<<c<<"] : ";
cin>>nos[r][c];
}
cout<<"Average : "<<(float)(nos[r][0]+nos[r][1]+nos[r][2])/3<<endl;
av = ((float)nos[r][0]+nos[r][1]+nos[r][2])/3;
if(av==0 || av<=74.99){
cout<<"grade = 5.0"<<endl; }
else if(av ==75 || av<=76.99){
cout<<"grade = 3.0"<<endl; }
else if(av ==77 || av<=79.99){
cout<<"grade = 2.75"<<endl; }
else if(av ==80 || av<=82.99){
cout<<"grade = 2.50"<<endl; }
else if(av ==83 || av<=84.99){
cout<<"grade = 2.25"<<endl; }
else if(av ==85 || av<=87.99){
cout<<"grade = 2.0"<<endl; }
else if(av ==88 || av<=90.99){
cout<<"grade = 1.75"<<endl; }
else if(av ==91 || av<=93.99){
cout<<"grade = 1.50"<<endl;}
else if(av ==94 || av<=96.99){
cout<<"grade = 1.25"<<endl; }
else(av ==97 || av<=100){
cout<<"grade = 1.0"<<endl;}
}
cout<<"\nTry Another [Y/N] : ";
cin>>l;
} while(l!='N' && l!='n');
getch();
}