我需要编写一个程序来获取用户的输入。第一个是名为 inputMonth 的 char,第二个是名为 inputDay 的 int。该程序需要比较输入并确定季节。范围是:
Spring :3月20日至6月20日 夏季:6月21日至9月21日 秋季:9月22日-12月20日 冬季:12月21日至3月19日
到目前为止,我已经尝试获取冬季月份,但程序没有输出。 4 月 11 日的示例输入将输出 Spring
#include <string.h>
int main(void) {
char inputMonth[50];
int inputDay;
scanf("%s", inputMonth);
scanf("%d", &inputDay);
if (inputMonth <= 3 && inputDay <= 19) {
printf("Winter");
}
return 0;
}
最佳答案
inputMonth
是一个字符串,您将其作为字符串读取,然后将其与数字进行比较(您应该在编译器中启用更多警告以捕获类似的错误)。
要么将 inputMonth
读取为数字(类似于 inputDay
),否则您将不得不重写月份比较(我建议您查看 strcmp
函数)。
关于c - 如何比较 C 中季节的字符输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69226686/