请设计一个程序,让用户可以输入年龄后,进行下列动作:(请参考「Lecture03作业3_年龄判别说明.mp4」视频) 判断为哪一个年龄层的人,其中1~5岁为kid,6~17岁为teenager,18岁(含)以上为adult;并将判断结果打印出来。 用户可一直连续输入,而程序不会中断。 若用户输入的为非数字,则持续请用户输入至数字为止。 若使用者输入的为end的文字,无论大小写,皆可结束程序。
时间: 2023-03-29 17:02:47 浏览: 80
我可以回答这个问题。以下是程序设计:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main() {
int age;
char input[10];
while (1) {
printf("请输入年龄:");
fgets(input, 10, stdin);
if (tolower(input[]) == 'e' && tolower(input[1]) == 'n' && tolower(input[2]) == 'd') {
printf("程序结束。\n");
break;
}
if (!isdigit(input[])) {
printf("输入错误,请重新输入。\n");
continue;
}
age = atoi(input);
if (age < 1) {
printf("输入错误,请重新输入。\n");
continue;
}
if (age <= 5) {
printf("kid\n");
} else if (age <= 17) {
printf("teenager\n");
} else {
printf("adult\n");
}
}
return ;
}
用户可以输入年龄,程序会判断年龄属于哪个年龄层,并将判断结果打印出来。如果用户输入的不是数字,则程序会提示输入错误,并要求重新输入。如果用户输入的是end,则程序会结束。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)