#include <stdio.h> #include <stdlib.h> int main() { int a; printf("请输入你的成绩:"); scanf("%d",&a); printf("你的等级是:"); switch(a/10) { case 10:printf("A\n");break; case 9:printf("A\n");break; case 8:printf("B\n");break; case 7:printf("C\n");break; case 6:printf("D\n");break; case 5:printf("E\n");break; case 4: case 3: case 2: case 1: case 0: default:printf("你输入的成绩有误\n"); } system("pause"); return 0; }
时间: 2024-03-19 10:42:35 浏览: 21
这段代码也是使用 `switch` 语句完成成绩百分制转换成等级的功能,与之前的代码相比,有以下几点区别:
1. 输出的是等级,而不是五分制成绩;
2. 在 `case` 语句中,使用了多个情况共用同一个代码块的方式,可以减少代码的冗余;
3. 使用了 `system("pause")` 等待用户的输入,这是一个 Windows 平台的特有函数,可以让控制台窗口停留在屏幕上,直到用户按下任意键继续执行。
这段代码也是正确的,可以完成相应的功能。
相关问题
include <stdio.h>#include <stdlib.h>
这是一个C语言的标准头文件的引用,其中<stdio.h>包含了C语言的输入输出函数,而<stdlib.h>包含了一些通用的工具函数和内存分配函数。这些头文件在C语言中非常常见,几乎每个C程序都会用到它们。下面是一个简单的例子,展示了如何使用这两个头文件。
```c
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num;
char str[100];
printf("Enter an integer: ");
scanf("%d", &num);
printf("You entered %d.\n", num);
printf("Enter a string: ");
scanf("%s", str);
printf("You entered %s.\n", str);
return 0;
}
```
上面的程序使用了<stdio.h>中的printf和scanf函数,以及<stdlib.h>中的exit函数。输入一个整数和一个字符串,然后将它们输出到控制台上。
C语言中 绝对值 函数用 #include <stdio.h>如何表示
在C语言中,求绝对值的函数可以使用标准库函数`abs()`来实现。要使用`abs()`函数,需要在代码中包含头文件`<stdlib.h>`而不是`<stdio.h>`。
下面是一个示例代码,展示了如何使用`abs()`函数来求取一个整数的绝对值:
```c
#include <stdlib.h>
#include <stdio.h>
int main() {
int num = -10;
int absNum = abs(num);
printf("The absolute value of %d is %d\n", num, absNum);
return 0;
}
```
在上述代码中,我们首先包含了`<stdlib.h>`头文件,然后声明了一个整数变量`num`并赋值为-10。接下来,我们使用`abs()`函数来计算`num`的绝对值,并将结果赋值给`absNum`变量。最后,我们使用`printf()`函数将结果打印到控制台上。
相关推荐
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)