即使我输入其他日期,它也只会打印“星期日”。如何解决此问题? 我通常不使用int main(),但是我的一个朋友做了这种方法,它确实起作用了,但是我使用的是学校编写的程序,所以我不能直接粘贴它。但我很确定我是完全复制的
#include <stdio.h>
int date;
int main()
{
printf(" \n\n JUNE 2020 \n");
printf(" SUN MON TUE WED THU FRI SAT \n");
printf(" 1 2 3 4 5 6 \n");
printf(" 7 8 9 10 11 12 13 \n");
printf(" 14 15 16 17 18 19 20 \n");
printf(" 21 22 23 24 25 26 27 \n");
printf(" 28 29 30 \n\n\n");
printf("Here is your schedule for June 2020 \n");
printf("Please select a date: ");
scanf("%d", &date);
if (( date == 7 ) || ( date == 14 ) || ( date == 21 ) || ( date || 28 ))
{
printf("sunday! ");
}
else if (( date == 1 ) || ( date == 8 ) || ( date == 15 ) || ( date == 22 ) || ( date == 29 ))
{
printf("monday! ");
}
else if (( date == 2 ) || ( date == 9 ) || ( date == 16 ) || ( date == 23 ) || ( date == 30 ))
{
printf("tuesday! " );
}
else if (( date == 3 ) || ( date == 10 ) || ( date == 17 ) || ( date = 24 ))
{
printf("wednesday!");
}
else if (( date == 4 ) || ( date == 11 ) || ( date == 18 ) || ( date == 25 ))
{
printf("thursday!");
}
else if (( date == 5 ) || ( date == 12 ) || ( date == 19 ) || ( date == 26 ))
{
printf("friday!");
}
else if (( date == 6 ) || ( date == 13 ) || ( date == 20 ) || ( date == 27 ))
{
printf("saturday!");
}
return 0;
} // end
您的第一个if语句的最后一个条件有错误。
应该更改为:
Previously, it was evaluating
( date || 28 )
, which is true asdate
is a positive integer. This made the entire condition true.In your first if statement, you have
(date || 28)
which should have been(date == 28)
instead:The reason that it always runs only the first
if
statement is thatelse if
only gets checked if the aboveif
statement is not true. In your case, the first if statement is always true, because( date || 28 )
expression is always true.你的问题
Is always true and hence first
if
always true.改成
说明
Is equivalent to
if (date || true)
and it's always true.