#pragma warning(disable: 4996)
#include<stdio.h>
int main(){
double array1[8] = {0};
double data;
double lg_data=0;
int i = 0, j = 0;
int count = 1;
int lg_count = 1;
printf("7개의 양의 실수를 입력하세요.(마지막 원소는 자동으로 -1이 됩니다.)\n");
for (i = 0; i < 8; i++){
if (i == 7){
array1[i] = -1;
}
else{
scanf("%lf", &array1[i]);
}
}//end of loop
for (i = 0; i < 8; i++){
count = 1;
for (j = 0; j < i; j++){
if (array1[i] == array1[j]){
count++;
if (count >= lg_count){
lg_count = count;
data = array1[i];
if (data >= lg_data){
lg_data = data;
}
}
}
else{
if (array1[i] >= lg_data){
lg_data = array1[i];
lg_count = count;
}
}
}//end of for(j)
}//end of for(i)
printf("%lf %d\n", lg_data, lg_count);
return;
如果有符合 if (array1[i] == array1[j])
语句的值,我想停止 for 循环,即使仍然有符合 else 语句的值。我已经尝试将 break 语句放在 if 语句中......但它没有用。
我该如何解决这个问题?
最佳答案
一个选择是将两个嵌套循环拆分为一个单独的函数。通过从函数返回,执行将跳出两个 for 循环:
void loops() {
for (i = 0; i < 8; i++){
count = 1;
for (j = 0; j < i; j++){
if (array1[i] == array1[j]){
...
return;
...
}
}
}
}
...
loops()
...
关于c - 转义 C 中的 if 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32835690/