gpt4 book ai didi

c - 案例循环有问题

转载 作者:太空宇宙 更新时间:2023-11-04 07:15:24 25 4
gpt4 key购买 nike

请。我想要显示从“A”到“H”的选项,但是“默认无效选项”一直出现。我如何防止出现“默认无效选项”。如果你能帮助我,非常感谢。

void disp3Ave (void)
{
int day, total;
char j;
extern float PSI23ave[];
extern float PSI24ave[];
extern float PSI25ave[];
float average;
total=0;
printf("Day 23 or 24?\n");
scanf("%\d", &day);

if(day == 23)
{
printf("A: 0300.\n");
printf("B: 0600.\n");
printf("C: 0900.\n");
printf("D: 1200.\n");
printf("E: 1500.\n");
printf("F: 1800.\n");
printf("G: 2100.\n");
printf("H: 0000.\n");
scanf("%c", &j);

switch (j)
{
case 'a':
case 'A': total= PSI23ave[3] + PSI23ave[4] + PSI23ave[5];
average = total/3;
printf("3-hr average = %.1f\n", average);
break;
case 'b':
case 'B': for (j=0;j<24;j++) total= PSI23ave[6] + PSI23ave[7] + PSI23ave[8];
average = total/3;
printf("3-hr average = %.1f\n", average);
break;
case 'c':
case 'C': for (j=0;j<24;j++) total= PSI23ave[9] + PSI23ave[10] + PSI23ave[11];
average = total/3;
printf("3-hr average = %.1f\n", average);
break;
case 'd':
case 'D': for (j=0;j<24;j++) total= PSI23ave[12] + PSI23ave[13] + PSI23ave[14];
average = total/3;
printf("3-hr average = %.1f\n", average);
break;
case 'e':
case 'E': for (j=0;j<24;j++) total= PSI23ave[15] + PSI23ave[16] + PSI23ave[17];
average = total/3;
printf("3-hr average = %.1f\n", average);
break;
case 'f':
case 'F': for (j=0;j<24;j++) total= PSI23ave[18] + PSI23ave[19] + PSI23ave[20];
average = total/3;
printf("3-hr average = %.1f\n", average);
break;
case 'g':
case 'G': for (j=0;j<24;j++) total= PSI23ave[21] + PSI23ave[22] + PSI23ave[23];
average = total/3;
printf("3-hr average = %.1f\n", average);
break;
case 'h':
case 'H': for (j=0;j<24;j++) total= PSI24ave[0] + PSI24ave[1] + PSI24ave[2];
average = total/3;
printf("3-hr average = %.1f\n", average);
break;

default: printf("Invalid option.\n");
break;

}
}

提前致谢!干杯!

最佳答案

看起来当您为 int 变量 day 输入值时,字符 '\n' 保留在输入缓冲区中。
这将存储在您的变量 j 中,因此您会得到 Invalid option。

编辑:你可以试试 scanf("\n%c", &j);

关于c - 案例循环有问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25299760/

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