用c语言编写一个switch小程序,输入一个数字,输出对应的星期的一个英文单词
时间: 2024-11-22 11:36:17 浏览: 29
在C语言中,你可以创建一个简单的switch结构来实现这个功能。下面是一个示例程序,它会根据用户输入的整数(1-7代表一周的天数)输出相应的英文星期:
```c
#include <stdio.h>
int main() {
int day;
printf("请输入一个数字(1-7),表示星期几:");
scanf("%d", &day);
// 定义一周的英文星期
const char *weekdays[] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
// 使用switch语句判断并输出对应英文单词
switch (day) {
case 1:
printf("The day is %s.\n", weekdays[0]);
break;
case 2:
printf("The day is %s.\n", weekdays[1]);
break;
case 3:
printf("The day is %s.\n", weekdays[2]);
break;
case 4:
printf("The day is %s.\n", weekdays[3]);
break;
case 5:
printf("The day is %s.\n", weekdays[4]);
break;
case 6:
printf("The day is %s.\n", weekdays[5]);
break;
case 7:
printf("The day is %s.\n", weekdays[6]);
break;
default:
printf("Invalid input! Please enter a number between 1 and 7.\n");
break;
}
return 0;
}
```
阅读全文