我的代码出了什么问题 [英] Whats wrong with my code

查看:98
本文介绍了我的代码出了什么问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须为GPA转换器制作代码。当我输入一个像83这样的数字时,每件事情都能正常工作。但是当我输入一个像小数字一样的数字83.5时,它会为我输出else消息。我错过了什么?



我的尝试:



< pre lang =c ++> #include < < span class =code-leadattribute> iostream >
#include < string >
使用 命名空间 std ;

int main()
{

float 等级;
cout.precision( 2 );

cout<< GPA转换<< ENDL;
cout<< --------------<< ENDL;
cout<< 输入您的成绩:;
cin>>年级;
cout<< ENDL;

if (等级< = 100 && grade> = 96
cout<< 您将收到一个字母等级A,其GPA为4.00。;

else if (grade< = 95 && grade> = 90
cout<< 您将收到字母等级A-,GPA为3.70。;

else if (grade< = 89 && grade> = 87
cout<< 您将收到B +的字母等级,其GPA为3.30。;

else if (grade< = 86 && grade> = 84
cout<< 您将收到B级的字母等级,其GPA为3.00。;

else if (grade< = 83 && grade> = 80
cout<< 您将收到字母等级为B-,GPA为2.70。;

else if (grade< = 79 && grade> = 77
cout<< 您将收到C + + 2.30 GPA的字母等级。;

else if (grade< = 76 && grade> = 74
cout<< 您将收到一个字母等级为C且2.00 GPA。;

else if (grade< = 73 && grade> = 70
cout<< 您将收到一个C级的字母等级,其GPA为1.70。;

else if (grade< = 69 && grade> = 67
cout<< 您将收到D +的字母等级,其GPA为1.30。;

else if (grade< = 66 && grade> = 64
cout<< 您将收到一个字母等级为D且1.00 GPA。;

else if (grade< = 63 && grade> = 60
cout<< 您将收到D-的字母等级,0.70 GPA。;

else if (grade< = 59 && grade> = 0
cout<< 您将收到一个字母等级为F且0.00 GPA。;

else
cout<< 您尚未输入0到100之间的成绩。结束程序。;


return 0 ;
}< / string>< / iostream>

解决方案

您应该学会尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到有一点它会停止你所期望的。

在Visual Studio 2010中掌握调试 - 初学者指南 [ ^ ]

http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html [ ^ ]

https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html [ ^ ]



你自己陷阱,看看你的代码

  else   if (等级< =  86 &&等级> =  84 
cout<< 您将收到B级的字母等级,其GPA为3.00。;

else if (grade< = 83 && grade> = 80
cout<< 您将收到字母等级为B-,GPA为2.70。;



83.5不会收到3.00,因为它不在86和84之间。

83.5不会收到2.70因为它不在83到80之间。



通过在调试器上逐行运行代码,你会发现问题出在哪里。


如果等级真的是一个浮点数,那么你必须让你的范围完整,例如

引用:

if(等级< = 100&& grade> = 96)

cout<< 你将获得一个4.00 GPA的A级字母。;



if(等级< = 95&& grade> = 90)

cout<< 你将收到一个A级的字母等级为3.70 GPA。;



必须改为

  if (grade< =  100  .0f&& grade> =  96  .0f)
cout<< 您将收到一个字母等级A,其GPA为4.00。;

else if (grade> = 90 .0f) // 您无需检查(等级< 96.0) f)这里
cout<< 您将收到字母等级A-,GPA为3.70。;





等等。


i had to make a code for a GPA converter. every thing works fine when I put in a number like 83. But when I input a number with a decimal like 83.5 it prints out the else message for me. What am I missing?

What I have tried:

#include <iostream>
#include <string>
using namespace std;

int main()
{

    float grade;
    cout.precision(2);

    cout << "GPA Conversion" << endl;
    cout << "--------------" << endl;
    cout << "Enter your grade: ";
    cin >> grade;
    cout << endl;

    if ( grade <=100 && grade  >=96)
        cout << "You will receive a letter grade of A with a 4.00 GPA.";

    else if ( grade <=95 && grade  >=90)
        cout << "You will receive a letter grade of A- with a 3.70 GPA.";

    else if ( grade <=89 && grade  >=87)
        cout << "You will receive a letter grade of B+ with a 3.30 GPA.";

    else if ( grade <=86 && grade  >=84)
        cout << "You will receive a letter grade of B with a 3.00 GPA.";

    else if ( grade <=83 && grade  >=80)
        cout << "You will receive a letter grade of B- with a 2.70 GPA.";

    else if ( grade <=79 && grade  >=77)
        cout << "You will receive a letter grade of C+ with a 2.30 GPA.";

    else if ( grade <=76 && grade  >=74)
        cout << "You will receive a letter grade of C with a 2.00 GPA.";

    else if ( grade <=73 && grade  >=70)
        cout << "You will receive a letter grade of C- with a 1.70 GPA.";

    else if ( grade <=69 && grade  >=67)
        cout << "You will receive a letter grade of D+ with a 1.30 GPA.";

    else if ( grade <=66 && grade  >=64)
        cout << "You will receive a letter grade of D with a 1.00 GPA.";

    else if ( grade <=63 && grade  >=60)
        cout << "You will receive a letter grade of D- with a 0.70 GPA.";

    else if ( grade <=59 && grade  >=0)
        cout << "You will receive a letter grade of F with a 0.00 GPA.";

    else
        cout << "You have not entered a grade between 0 and 100. Ending program.";


return 0;
}</string></iostream>

解决方案

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

You trap yourself, look at your code

else if ( grade <=86 && grade  >=84)
    cout << "You will receive a letter grade of B with a 3.00 GPA.";

else if ( grade <=83 && grade  >=80)
    cout << "You will receive a letter grade of B- with a 2.70 GPA.";


83.5 don't receive a 3.00 because it is not between 86 and 84.
83.5 don't receive a 2.70 because it is not between 83 and 80.

By running your code line by line on debugger, you would see where is the problem by yourself.


If grade is really a float then you have to make your ranges complete, e.g.

Quote:

if ( grade <=100 && grade >=96)
cout << "You will receive a letter grade of A with a 4.00 GPA.";

else if ( grade <=95 && grade >=90)
cout << "You will receive a letter grade of A- with a 3.70 GPA.";


must be instead

if ( grade <=100.0f && grade  >=96.0f)
        cout << "You will receive a letter grade of A with a 4.00 GPA.";
 
else if ( grade  >=90.0f) // you don't need to check for  (grade < 96.0f) here
        cout << "You will receive a letter grade of A- with a 3.70 GPA.";



and so on.


这篇关于我的代码出了什么问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆