c - 转义 C 中的 if 语句

标签 c loops if-statement for-loop

#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/

相关文章:

c - C 中 openssl RSA_public_encrypt() 上的段错误

c - 在 C 函数中删除链表节点不会传输到调用函数

c - 我正在尝试创建一个函数来接受用户输入的回文。为什么我输入一个数组后,它会自动输入其余的数组?

javascript - 动态更改 <div> 元素的大小

c - 如何在 Ubuntu 16.04 的 Codeblocks 13.12 中包含或链接 ncurses/curses 库?

c - C语言中char word[100]是什么意思

javascript - Coffeescript 翻译 - 嵌套 for 循环

VBA 转到下一个过滤的单元格

batch-file - 在 "IF EXIST"内,等待两个小时然后退出

windows - 比较 Windows 批处理文件中的 2 个日期