1

所以这个程序应该根据你输入的百分比告诉你你的成绩,我基本上只使用 if 和 else 语句来编码它。该程序一直运行到 59%,然后当我输入高于该百分比的任何内容时,它就不起作用了。因为在程序中只是不会告诉我 59% 后的成绩。谢谢任何事情都会有帮助!!!

ps 我知道可能有一种更简单的方法可以编写这个程序,但我想练习 if 和 else 语句......

//
//exercise 1.
//you will enter in your percent and it will anounce your grade.
//create a program so that it will notify the user of their letter grade
//0-59 F 60-69 D 70-79 C 80-89 B 90-100 A
//a=user input
//For whatever reason the program seems to only work up to 59% and after that it doesn't work.



int a;
#include <iostream>
using namespace std;
int main()
{
cout<< "Enter in the percent of your grade and \n I will tell you your grade"<<endl;
cin>>a;
if(a==100)
{
    cout<<"you scored a perfect A";
}
else 
    if(a<=59)
    {
        if(a<0)
        {
            cout<<"your really stupid";
        }
        else
            cout<<"you failed";
    }
    else
        if(a>=60)
        {
            if(a<=69)
            {
                cout<<"You got a D";
            }
        }
        else
            if(a>=70)
            {
                if(a<=79)
                {
                    cout<<"you got a C";
                }
            }
            else
                if(a>=80)
                {
                    if(a<=89)
                    {
                        cout<<"you got a B";
                    }
                }
                else
                    if(a>=90)
                    {


                            cout<<"you got an A";

                    }


}
4

3 回答 3

1

问题是您正在检查 a >= 60... 它是什么(假设 a = 75)。但是,它不会被任何其他条件语句捕获。通过代码中的以下注释更好地解释。

#include <iostream>
using namespace std;

int a;
int main()
{
cout<< "Enter in the percent of your grade and \n I will tell you your grade"<<endl;
cin>>a;
if(a==100)
{
    cout<<"you scored a perfect A";
}
else 
    if(a<=59)
    {
        if(a<0)
        {
            cout<<"your really stupid";
        }
        else
            cout<<"you failed";
    }
    else
        if(a>=60) // 75 >= 60
        {
            if(a<=69) // But 75 is > 69
            {
                cout<<"You got a D";
            }
        }
        // ONLY REACHES THIS POINT IF a < 60
        else
            if(a>=70)
            {
                if(a<=79)
                {
                    cout<<"you got a C";
                }
            }
            else
                if(a>=80)
                {
                    if(a<=89)
                    {
                        cout<<"you got a B";
                    }
                }
                else
                    if(a>=90)
                    {
                            cout<<"you got an A";

                    }
}

这可能是一个更好的方法:

int main()
{
    cout<< "Enter in the percent of your grade and \n I will tell you your grade"<<endl;
    cin>>a;
    if(a==100)
    {
        cout<<"you scored a perfect A";
    }
    else 
    {
        if(a<=59)
        {
            if(a<0)
            {
                cout<<"your really stupid";
            }
            else
                cout<<"you failed";
            }
        else
        {
            if (a >= 60 && a <= 69)
            {
                cout << "D";
            }
            else if (a >= 70 && a <= 79)
            {
                cout << "C";
            }
            else if (a >= 80 && a <= 89)
            {
                cout << "B";
            }
            else
            {
                cout << "A";
            }
        }
    }
}
于 2013-01-09T19:03:28.620 回答
0

我认为您在第一个 else 之后缺少括号。

试试这个:

//
//exercise 1.
//you will enter in your percent and it will anounce your grade.
//create a program so that it will notify the user of their letter grade
//0-59 F 60-69 D 70-79 C 80-89 B 90-100 A
//a=user input
//For whatever reason the program seems to only work up to 59% and after that it doesn't work.



int a;
#include <iostream>
using namespace std;
int main()
{
cout<< "Enter in the percent of your grade and \n I will tell you your grade"<<endl;
cin>>a;
if(a==100)
{
    cout<<"you scored a perfect A";
}
else
{ 
    if(a<=59)
    {
        if(a<0)
        {
            cout<<"your really stupid";
        }
        else
            cout<<"you failed";
    }
    else
        if(a>=60)
        {
            if(a<=69)
            {
                cout<<"You got a D";
            }
        }
        else
            if(a>=70)
            {
                if(a<=79)
                {
                    cout<<"you got a C";
                }
            }
            else
                if(a>=80)
                {
                    if(a<=89)
                    {
                        cout<<"you got a B";
                    }
                }
                else
                    if(a>=90)
                    {


                            cout<<"you got an A";

                    }

}
} // end main
于 2013-01-09T19:04:19.593 回答
0

在您的代码中,一旦您到达if(a>=60),如果 的值a不小于 70,则不输出任何内容,并且代码退出条件,因为它找到了一个真实的条件。至于为什么你不能让它在 59 以上做任何事情,我不知道,因为它看起来应该可以工作。但是,通过将您的内部 if 块与其父块组合,您可以获得要检查的代码是否a在 10 个百分点的组内,如果它不在该组中,则继续下一个。以下是您将如何执行此操作的示例。

//
//exercise 1.
//you will enter in your percent and it will anounce your grade.
//create a program so that it will notify the user of their letter grade
//0-59 F 60-69 D 70-79 C 80-89 B 90-100 A
//a=user input
//For whatever reason the program seems to only work up to 59% and after that it doesn't work.



int a;
#include <iostream>
using namespace std;
int main()
{
  cout<< "Enter in the percent of your grade and \n I will tell you your grade"<<endl;
  cin>>a;
  if(a==100)
  {
      cout<<"you scored a perfect A";
  }
  else if(a<=59)
  {
      if(a<0)
      {
          cout<<"your really stupid";
      }
      else
      {
          cout<<"you failed";
      }
  }
  else if(a>=60 && a<=69)
  {
      cout<<"You got a D";
  }
  else if(a>=70 && a<=79)
  {
      cout<<"you got a C";
  }
  else if(a>=80 && a<=89)
  {
      cout<<"you got a B";
  }
  else if(a>=90)
  {
      cout<<"you got an A";
  }
}
于 2013-01-09T19:13:38.027 回答