c-我的If/Else语句在错误的时间打印(更新)



当我输入食物和一天时,我的程序应该找出正确的食物。如果用户放入正确的食物,我的程序应该说:

"嗯。。。今天等不及要吃[食物名称]了">

相反,我的程序一直在说:

"对不起,在[天]我不能吃那个">

在我输入一种食物和一天之后。这是我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char**argv)
{
char day[20];
char food[20];
int numFood;
printf("Enter food: ");
fgets(food, 20, stdin);
printf("Enter day: ");
scanf("%s", day);
//determines what food the picky eater would eat
if((strcmp(day, "Sunday") == 0 || strcmp(day, "Monday") == 0 || strcmp(day, "Tuesday") == 0) && (food[0] != 'm' && food[0] != 'k'))
{
printf("Mmmm...can't wait to eat %s today!!!n", food);
printf("Enter the number of %s you can to eat: ", food);
scanf("%d", &numFood);
if(numFood > 3)
{
printf("That's a lot of %s!", food);
exit(0);
}
}
else
{
printf("Sorry, on Sundays/Mondays/Tuesdays I can't eat that...");
exit(0);
}
if((strcmp(day, "Wednesday") == 0 || strcmp(day, "Thursday") == 0 || strcmp(day, "Friday") == 0) && food[0] != 'j')
{
printf("Mmmm...can't wait to eat %s today!!!", food);
exit(0);
} else {
printf("Sorry, on Wednesday/Thursday/Friday I can't eat that...");
exit(0);
}

if(strcmp(day, "Saturday") == 0 && strlen(day) <= 7 && food[0] == 'p')
{
printf("nMmmmm...can't wait to eat %s today!!!", food);
exit(0);
} else {
printf("nSorry, on Saturdays I can't eat that...");
}
return 0;
}

好的,现在我看到你的问题

给你

if ((strcmp(day, "Sunday") == 0 || strcmp(day, "Monday") == 0 || strcmp(day, "Tuesday") == 0) && (food[0] != 'm' && food[0] != 'k'))
{
printf("Mmmm...can't wait to eat %s today!!!n", food);
.......
}
else
{
printf("Sorry, on Sundays/Mondays/Tuesdays I can't eat that...");
exit(0);
}

(省略中间的内容(。所以,要么你匹配第一个复杂的问题,要么打印对不起。

我怀疑你想要

if ((strcmp(day, "Sunday") == 0 || strcmp(day, "Monday") == 0 || strcmp(day, "Tuesday") == 0))
{
if(food[0] != 'm' && food[0] != 'k')
{
printf("Mmmm...can't wait to eat %s today!!!n", food);
printf("Enter the number of %s you can to eat: ", food);
scanf("%d", &numFood);
if (numFood > 3)
{
printf("That's a lot of %s!", food);
exit(0);
}
}
else
{
printf("Sorry, on Sundays/Mondays/Tuesdays I can't eat that...");
exit(0);
}
}

在大多数情况下,我认为您的代码正在做您期望它做的事情,但在if-else条件语句中有一些小问题。

除了添加n字符以使stdout更干净之外,我还让您的代码基本上保持不变,只是调整了前面提到的条件。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char**argv)
{
char day[20];
char food[20];
int numFood;
printf("Enter food: ");
fgets(food, 20, stdin);
printf("Enter day: ");
scanf("%s", day);
// I added a null-terminator for printing sakes.
size_t food_length = strlen(food);
food[food_length - 1] = '';
//determines what food the picky eater would eat
if((strcmp(day, "Sunday") == 0 || strcmp(day, "Monday") == 0 || strcmp(day, "Tuesday") == 0))
{
// Here I split-up your condition into 2 separate parts that prevent
// falling into the next block as you had it originally
if (food[0] != 'm' && food[0] != 'k') {
printf("Mmmm...can't wait to eat %s today!!!n", food);
printf("Enter the number of %s you can to eat: ", food);
scanf("%d", &numFood);
if(numFood > 3)
{
printf("That's a lot of %s!n", food);
exit(0);
}
} else {
printf("Sorry, on Sundays/Mondays/Tuesdays I can't eat that...n");
exit(0);
}
/*
*So now instead of having your `if-else` print out on everyday entered
*thats *not* matched once you find your day using `strcmp` you enter
*that block based off the condition.
*/
} else if ((strcmp(day, "Wednesday") == 0 || strcmp(day, "Thursday") == 0 || strcmp(day, "Friday") == 0))
{
if (food[0] != 'j') {
printf("Mmmm...can't wait to eat %s today!!!n", food);
exit(0);
} else {
/*
*Here for instance you would always print out this part, on say input
*of `Sunday` where if there aren't `if-else` checks but using separate
*`if` blocks like you had the `else` would always be met on days that
*weren't matched
*/
printf("Sorry, on Wednesday/Thursday/Friday I can't eat that...n");
exit(0);
}
}
else if(strcmp(day, "Saturday") == 0)
{ 
if (food[0] == 'p') {
printf("Mmmmm...can't wait to eat %s today!!!n", food);
exit(0);
} else {
printf("Sorry, on Saturdays I can't eat that...n");
}
}
return 0;
}

你可以看到我在你的代码中添加了注释,你需要通过在食物和一周中的某一天之间分配检查来设置条件。更重要的是使用else-if,在此之前,您使用了单独的if-else块,这些块将在找到星期一后触发。如果你现在不明确退出这个区块,你就知道今天是星期一,所以你不会触发星期三条件,但有一个单独的if-else,所以你的else每次都会像你提到的那样打印出来。

最新更新