首页 > 解决方案 > 如何只显示一次消息?

问题描述

我有 2 个结构数组。我需要检查每个 buff 数组的每个节奏数组,如果找到匹配项,我将打破整个循环并输出消息“找到匹配项”一次并退出。否则应输出“不匹配”。

如何在相互检查两个数组后只显示一次消息?我需要在比赛结束后跳出循环。否则输出不匹配。输出被多次显示。

for (int g = 0; g < lines; g++) {
    for (int y = 0; y < lines; y++) {
        if ((strstr(tempo[g], buff[y])) != NULL) //Cheking if enter username and Password Exist in record file.
        {
            printf("A match found on line");
            break;

        } else
        {
            printf("Mismatch in Username or Password");
        }
    }
}

标签: carrays

解决方案


使用变量found作为标志来指示何时找到匹配项。您可以使用测试该变量,break如果它设置为1.

然后移动print循环外部并使用标志来确定要打印的内容。

int found = 0;
for (int g = 0; g < lines; g++) {
    for (int y = 0; y < lines; y++) {
        if ((strstr(tempo[g], buff[y])) != NULL) {
            found = 1;
            break;
        }
    }
    if (found) {
        break;
    }
}

if (found) //Cheking if enter username and Password Exist in record file.
{
    printf("A match found on line");
} else
{
    printf("Mismatch in Username or Password");
}

推荐阅读