gpt4 book ai didi

c - 在函数中递增案例编号?

转载 作者:太空狗 更新时间:2023-10-29 16:01:52 28 4
gpt4 key购买 nike

我刚开始学习 C,但我无法让我的代码在同一个 printf 函数上递增 dayName。

这是我的代码:

#include <stdio.h>
#define LENGH_OF_WEEK 7

int main()
{
int daysOfWeek[LENGH_OF_WEEK] = {0,1,2,3,4,5,6};
char* dayName = NULL;

int i;
for (i = 0; i < LENGH_OF_WEEK; i++)
{
switch (daysOfWeek[i])
{
case 0: dayName = "Sunday"; break;
case 1: dayName = "Monday"; break;
case 2: dayName = "Tuesday"; break;
case 3: dayName = "Wednesday"; break;
case 4: dayName = "Thursday"; break;
case 5: dayName = "Friday"; break;
case 6: dayName = "Saturday"; break;
}

printf("%s, %s Happy Days\n", dayName, dayName);
}

return 0;
}

如您所见,我正试图让我的控制台唱快乐的日子,但它只说了两次 dayName,我想弄清楚如何让它说“星期六,多么棒的一天!一切都在欢呼陪你一周!”对于案例 6。

最佳答案

这是我的经典主题曲版本。没有使用 switch 语句,抱歉

#include <stdio.h>


char *day[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
int main()
{

int i;
for (i = 0; i < 5; i+=2)
{
printf("%s, %s Happy Days\n", day[i], day[i+1]);
}
printf("Saturday, what a day\n Groovin' all week with you!\n");

return 0;
}

关于c - 在函数中递增案例编号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20869125/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com