不给出最后一行的平均值和错误[返回0] [英] not giving average and error in last line [return 0]

查看:82
本文介绍了不给出最后一行的平均值和错误[返回0]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 printf( 您的成绩是); 
if (avg> 90)
{
printf( A);
}
else if (avg> 85&& avg< = 89
{
printf( A - );
}
else if (avg> 80&& avg< = 84
{
printf( B +);
}
else if (avg> 75&& avg< = 79
{
printf( B);
}
else if (avg> 70&& avg< = < span class =code-digit> 74

{
printf( 乙 - );
}
其他 如果(平均值> 65&& avg< = < span class =code-digit> 69

{
printf( C +);
}
else if (avg> 60&& avg< = < span class =code-digit> 64

{
printf( C);
}
其他 如果(平均值>&& avg< = < span class =code-digit> 59

{
printf( ç - );
}
其他 如果(平均值>&& avg< = < span class =code-digit> 54

{
printf( d);
}
else
printf( F);

解决方案

首先查看代码。

  if (avg> 90)
...
else if (avg> 85&& avg< = 89



假设 avg 是一个整数,你不需要检查小于或等于到89,因为之前的测试已经做到了!

所以它变得更清晰,更容易阅读:

  if (平均值> 90)
...
else if (平均值> 85)



像这样完成整个代码(并正确缩进,因此更容易读):

 printf( 您的成绩是); 
if (avg> 90)
...
else if (avg> 85)
...
else if (avg> 80)
...
else if (平均值> 75)
...
else if (平均值> 70)
...
其他 如果( avg> 65)
...
else if (avg> 60)
...
其他 如果(平均值> 55)
...
其他 如果(平均值> 55)
...
else
...

很明显,你有一个分支它不能采取:第二个> 55比较。


我认为现在是时候停止猜测你的代码在做什么了。是时候看到你的代码正在执行并确保它能达到预期的效果。



调试器是你的朋友。它会告诉你你的代码到底在做什么。

一步一步地执行,检查变量,你会发现它有一个停止做你期望的点。

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



- 学习一种或多种分析方法,我建议 EW Djikstra自上而下的分析方法,这是一个良好的开端。

https ://en.wikipedia.org/wiki/Top-down_and_bottom-up_design [ ^ ]

https://en.wikipedia.org/ wiki / Structured_programming [ ^ ]

https://en.wikipedia.org/wiki/Edsger_W._Dijkstra [https://www.cs.utexas.edu/users/EWD/ewd03xx/EWD316.PDF [ ^ ]



没有知识的捷径,没有人可以为你学习,你是唯一可以做到的人。

printf("Your Grade is ");
if(avg>90)
{
    printf("A");
}
else if(avg>85 && avg<=89)
{
    printf("A-");
}
else if(avg>80 && avg<=84)
{
    printf("B+");
}
else if(avg>75 && avg<=79)
{
    printf("B");
}
else if(avg>70&& avg<=74)
{
    printf("B-");
}
else if(avg>65&& avg<=69)
{
    printf("C+");
}
else if(avg>60&& avg<=64)
{
    printf("C");
}
else if(avg>55&& avg<=59)
{
    printf("C-");
}
else if(avg>55&& avg<=54)
{
    printf("D");
}
else
    printf("F");

解决方案

Start by looking at your code.

if(avg>90)
   ...
else if(avg>85 && avg<=89)


Assuming that avg is an integer, you don't need to check for "less than or equal to 89" because the test before that did that already!
So it gets a little cleaner and easier to read:

if(avg>90)
   ...
else if(avg>85)


Do the whole of your code like that (and indent it properly, so it's easier to read):

printf("Your Grade is ");
if(avg>90)
   ...
else if(avg>85)
   ...
else if(avg>80)
   ...
else if(avg>75)
   ...
else if(avg>70)
   ...
else if(avg>65)
   ...
else if(avg>60)
   ...
else if(avg>55)
   ...
else if(avg>55)
   ...
else
   ...

And it's obvious that you have a branch it can't take: the second > 55 comparison.


I think it is time for you to stop guessing what your code is doing. It is time to see your code executing and ensuring that it does what you expect.

The debugger is your friend. It will show you what your code is really doing.
Follow the execution step by step, 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[^]

- Learn one or more analyse methods, I recommend E.W. Djikstra top-Down analyse method, it is a good start.
https://en.wikipedia.org/wiki/Top-down_and_bottom-up_design[^]
https://en.wikipedia.org/wiki/Structured_programming[^]
https://en.wikipedia.org/wiki/Edsger_W._Dijkstra[^]
https://www.cs.utexas.edu/users/EWD/ewd03xx/EWD316.PDF[^]

There is no shortcut to knowledge, no one can learn for you, you are the only one that can do it.


这篇关于不给出最后一行的平均值和错误[返回0]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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