在大小写中使用整数。在C中使用DevC ++ [英] Using integer in case. Using DevC++ in C

查看:172
本文介绍了在大小写中使用整数。在C中使用DevC ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究一个检查学生成绩的应用程序。下面给出了这样的例子:



 #include< stdio.h> 
int main()
{
int mark;
printf( 请输入您的成绩:);
scanf( %d,& mark);

if (标记> = 90
{
printf( A );
}
其他 if (标记> = 80
{
printf( B);
}
其他 if (标记> = 70
{
printf( C);
}
其他 if (标记< = 69

printf( < span class =code-string> F);
getch();

return 0 ;
}





我想使用switch case语句编写相同的代码。



有谁能告诉我或者给我一个关于如何用case语句做同样事情的例子?

解决方案

你不能用用于C,C ++或C#范围的开关 - 仅用于可以测试完全相等的实际值。



您可以通过创建返回的函数来模拟它(例如)一个包含成员LowRange,MidRange和HighRange的枚举,你可以在switch语句中使用它,但即使这样,该函数也会归结为你已经拥有的代码。

I have been working on an application which checks for the grade of the student. Example of such is given below:

#include <stdio.h>
int main ()
{
    int mark;
    printf("Pls enter your grade: ");
    scanf ("%d", &mark);

    if (mark >=90)
              {
              printf("A");
              }
              else if (mark >= 80)
              {
              printf("B");
              }
              else if (mark >=70)
              {
              printf("C");
              }
              else if (mark <=69)

               printf("F");
              getch();

              return 0;
              }



I want to code the same thing but using switch case statement.

Can anyone tell me or give me an example on how to do the same thing above with case statement?

解决方案

You can't use a switch for ranges in C, C++ or C# - only for actual values which can be tested fro exact equality.

You could simulate it, by creating a function which returned (for example) an enum with members "LowRange", "MidRange" and "HighRange" which you could then use in a switch statement, but even then, the function would boil down to pretty much the code you have already.


这篇关于在大小写中使用整数。在C中使用DevC ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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