0

编辑:这是 If-Else。请看看发生了什么。如果答案错误,有人可以告诉我如何返回吗?比如,不正确的答案,重新输入?

使用命名空间标准;

int main()
{

cout<<"Welcome to the Grade Database. Please insert your domain: " ;
cout<<"\n";
int d, n;
cin>>d;
cout<<"Now enter your total grade(between 0-100): " ;
cin>>n;
if (n>0 && n<59){
    cout<<"See you next year then :(" ;
    cout<<"F-"<<n;}
else if (n<60 && n>=69){
    cout<<"Well...you pass ;D" ;
    cout<<"E-"<<n<<"  ~"<<d;}
else if (n>70 && n<=79){
    cout<<"Better than the average!";
        cout<<"D-"<<n<<"  ~"<<d ;}
else if (n>80 && n<=89){
    cout<<"Very well sir!";
    cout<<"C-"<<n<<"  ~"<<d;}
else if (n>90 && n<=99){
    cout<<"Wow, amazing! One of the best!";
    cout<<"B-"<<n<<"  ~"<<d;}
else if(n==100){
    cout<<"Well, hello there Mr. Stephen Hawking.";
    cout<<"A-"<<n<<"  ~"<<d;}
else{
    cout<<"Invalid Entry.";}

return 0;

}

4

1 回答 1

5

switch在 C++ 中不支持范围或条件,只支持完全匹配。既然您有条件,请尝试使用ifand else,如下所示:

cin>>n;
if (n>0 && n<59) {
    cout<<"See you next year then :(" ;
    cout<<"F-"<<n;
}
else if (n>=60 && n<=69) {
    cout<<"Well...you pass ;D" ;
    cout<<"E-"<<n<<"  ~"<<d;
}
else if (n>=70 && n<=79) {
    cout<<"Better than the average!";
    cout<<"D-"<<n<<"  ~"<<d ;
}
else if (n>=80 && n<=89) {
    cout<<"Very well sir!";
    cout<<"C-"<<n<<"  ~"<<d;
}
else if (n>=90 && n<=99) {
    cout<<"Wow, amazing! One of the best!";
    cout<<"B-"<<n<<"  ~"<<d;
}
else if (n==100) {
    cout<<"Well, hello there Mr. Stephen Hawking.";
    cout<<"A-"<<n<<"  ~"<<d;
}
else {
    cout<<"Invalid Entry.";
}

您可能还需要一些换行符。简单地写cout <<第二次不会开始新的一行,看看std::endl.

于 2013-03-01T20:30:04.233 回答