c - 如何只显示一次消息?
问题描述
我有 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");
}
}
}
解决方案
使用变量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");
}
推荐阅读
- javascript - React Redux 删除数组对象
- django - 任何数据库更新都会刷新 React + Django 应用
- scala - IntelliJ 仅在我选择它时重新格式化代码(对于 Scala)
- android - android.hardware.camera2.CameraAccessException:相机设备已在使用中
- java - 在字符串中的值中查找变量 Android Studio, Java
- java - 使用用于 rest-high-level-client 的 java 代码将完成建议添加到索引
- android - Jackson 在反序列化期间更改日期
- yaml - 在 puppet 模板文件中加密私钥
- http - 通过 HTTP/S 发送数据:base64url 中的二进制或编码?
- elasticsearch - 在弹性搜索中查找与整个查询匹配的文档