如果语句条件未满足,但程序卡住了
If statement conditions unmet and yet program gets stuck
基本上条件都不满足,但是程序不会从main if语句进入else语句。我会给出它的要点,前面是 tl;dr 版本,下面是完整版本。
tl;dr 版本:
a = 1;
b = 2;
c = 3;
if(a == 1)
{
if(b == 1 && c == 1)
do x;
if(b == 2 && c == 2)
do y;
if(b == 3 && c == 3)
do z;
}
else
printf("Invalid\n");
基本上 none 较小的 if 语句条件得到满足,程序应该继续执行 else 语句并打印“无效”,但它不会。它什么都不做。实际程序见:
#include <cs50.h>
#include <stdio.h>
int checksum = 10;
int j = 16;
long first = 5;
long second = 6;
int main(void)
{
checksum %= 10;
if(checksum == 0)
{
if(j == 15 && first == 3 && (second == 4 || second == 7))
printf("AMEX\n");
if(j == 16 && first == 5 && (second == 1 || second == 2 || second == 3 || second == 4 || second == 5))
printf("MASTERCARD\n");
if((j == 16 || j == 13) && first == 4)
printf("VISA\n");
}
else
printf("INVALID\n");
}
您的程序仅在未满足更大的 if 语句时才打印“INVALID”。您需要通过添加另一个与较小的 if 语句条件关联的 else 来更改您的代码,如下所示:
#include <cs50.h>
#include <stdio.h>
int checksum = 10;
int j = 16;
long first = 5;
long second = 6;
int main(void)
{
checksum %= 10;
if(checksum == 0)
{
if(j == 15 && first == 3 && (second == 4 || second == 7))
printf("AMEX\n");
else if(j == 16 && first == 5 && (second == 1 || second == 2 || second == 3 || second == 4 || second == 5))
printf("MASTERCARD\n");
else if((j == 16 || j == 13) && first == 4)
printf("VISA\n");
else printf("INVALID\n");
}
else
printf("INVALID\n");
}
希望对您有所帮助,
乔瓦尼·帕切拉
如果您希望“让代码看起来更干净一些”,您可以使用一种称为“提前退出”的编程模式。基本思想是,对于每个“正确”答案,您 return 值。由于您还没有使用函数,您的程序将不得不提前退出;但是,通常函数 return 是“正确的”值,而不是程序 returning 如果它成功/失败。
#include <cs50.h>
#include <stdio.h>
#include <stdlib.h>
int checksum = 10;
int j = 16;
long first = 5;
long second = 6;
int main(void) {
checksum %= 10;
if(checksum == 0 && j == 15 && first = 3 && (second == 4 || second == 7) {
printf("AMEX\n");
return EXIT_SUCCESS;
}
if (checksum == 0 && j == 16 && first == 5 && (second == 1 || second == 2 || second == 3 || second == 4 || second == 5))
printf("MASTERCARD\n");
return EXIT_SUCCESS;
}
if (checksum == 0 && (j == 16 || j == 13) && first == 4)
printf("VISA\n");
return EXIT_SUCCESS;
}
printf("INVALID\n");
return EXIT_FAILURE;
}
int main(void) {
中的int
是程序的return代码。它被发送回终端,因此人们可以判断程序 运行 是否正确或遇到错误。 EXIT_SUCCESS
和 EXIT_FAILURE
是 C 常数,定义在(我认为 <stdlib.h>
).
此外,请考虑养成使用 int main(int argc, char* argv)
而不是 int main(void)
的习惯。即使您不立即使用参数参数,它也是一种键入 main
函数的方法,您永远不必更新。
基本上条件都不满足,但是程序不会从main if语句进入else语句。我会给出它的要点,前面是 tl;dr 版本,下面是完整版本。
tl;dr 版本:
a = 1;
b = 2;
c = 3;
if(a == 1)
{
if(b == 1 && c == 1)
do x;
if(b == 2 && c == 2)
do y;
if(b == 3 && c == 3)
do z;
}
else
printf("Invalid\n");
基本上 none 较小的 if 语句条件得到满足,程序应该继续执行 else 语句并打印“无效”,但它不会。它什么都不做。实际程序见:
#include <cs50.h>
#include <stdio.h>
int checksum = 10;
int j = 16;
long first = 5;
long second = 6;
int main(void)
{
checksum %= 10;
if(checksum == 0)
{
if(j == 15 && first == 3 && (second == 4 || second == 7))
printf("AMEX\n");
if(j == 16 && first == 5 && (second == 1 || second == 2 || second == 3 || second == 4 || second == 5))
printf("MASTERCARD\n");
if((j == 16 || j == 13) && first == 4)
printf("VISA\n");
}
else
printf("INVALID\n");
}
您的程序仅在未满足更大的 if 语句时才打印“INVALID”。您需要通过添加另一个与较小的 if 语句条件关联的 else 来更改您的代码,如下所示:
#include <cs50.h>
#include <stdio.h>
int checksum = 10;
int j = 16;
long first = 5;
long second = 6;
int main(void)
{
checksum %= 10;
if(checksum == 0)
{
if(j == 15 && first == 3 && (second == 4 || second == 7))
printf("AMEX\n");
else if(j == 16 && first == 5 && (second == 1 || second == 2 || second == 3 || second == 4 || second == 5))
printf("MASTERCARD\n");
else if((j == 16 || j == 13) && first == 4)
printf("VISA\n");
else printf("INVALID\n");
}
else
printf("INVALID\n");
}
希望对您有所帮助,
乔瓦尼·帕切拉
如果您希望“让代码看起来更干净一些”,您可以使用一种称为“提前退出”的编程模式。基本思想是,对于每个“正确”答案,您 return 值。由于您还没有使用函数,您的程序将不得不提前退出;但是,通常函数 return 是“正确的”值,而不是程序 returning 如果它成功/失败。
#include <cs50.h>
#include <stdio.h>
#include <stdlib.h>
int checksum = 10;
int j = 16;
long first = 5;
long second = 6;
int main(void) {
checksum %= 10;
if(checksum == 0 && j == 15 && first = 3 && (second == 4 || second == 7) {
printf("AMEX\n");
return EXIT_SUCCESS;
}
if (checksum == 0 && j == 16 && first == 5 && (second == 1 || second == 2 || second == 3 || second == 4 || second == 5))
printf("MASTERCARD\n");
return EXIT_SUCCESS;
}
if (checksum == 0 && (j == 16 || j == 13) && first == 4)
printf("VISA\n");
return EXIT_SUCCESS;
}
printf("INVALID\n");
return EXIT_FAILURE;
}
int main(void) {
中的int
是程序的return代码。它被发送回终端,因此人们可以判断程序 运行 是否正确或遇到错误。 EXIT_SUCCESS
和 EXIT_FAILURE
是 C 常数,定义在(我认为 <stdlib.h>
).
此外,请考虑养成使用 int main(int argc, char* argv)
而不是 int main(void)
的习惯。即使您不立即使用参数参数,它也是一种键入 main
函数的方法,您永远不必更新。