gpt4 book ai didi

有人可以解决这个代码块吗?

转载 作者:行者123 更新时间:2023-11-30 18:37:49 24 4
gpt4 key购买 nike

帮我解决这个问题,因为我真的不知道

#include <stdio.h>
#include <stdlib.h>

int main() {
char ans;

printf("1. The patients felt _____ after taking the medicine.\n");
printf("\t a. best\n\t b. better\n\t c. good\n\n");
scanf("%c", &ans);

if (ans == 'b') {
printf("2. I ______ my essay by the time the bell rings.\n");
printf("\t a. have done\n\t b. shall do\n\t c. shall have done\n\n");
scanf("%c", &ans);
} else {
printf("YOU FAILED!");
};
return 0;
}

如果您回答了第一个问题,您将继续下一步并回答第二个问题,但问题是即使有 scanf,我也无法输入答案。

最佳答案

#include <stdio.h>
#include <stdlib.h>

int main() {
char ans;

printf("1. The patients felt _____ after taking the medicine.\n");
printf("\t a. best\n\t b. better\n\t c. good\n\n");
scanf("%c", &ans);

if (ans == 'b') {
printf("2. I ______ my essay by the time the bell rings.\n");
printf("\t a. have done\n\t b. shall do\n\t c. shall have done\n\n");
scanf(" %c", &ans);
} else {
printf("YOU FAILED!");
};

return 0;
}

区别在于:

scanf(" %c",&ans);
^ this space this will read whitespace characters (which newline also is)
until it finds a non space character.

scanf 没有消耗第一次 scanf 调用时留在缓冲区中的 \n 字符。

我尝试了这个,它有效!

关于有人可以解决这个代码块吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35826422/

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