【问题标题】:How to stack the output for every loop in C如何堆叠C中每个循环的输出
【发布时间】:2020-04-09 00:56:06
【问题描述】:

我试图在这个程序中仅使用基本循环将所有输出堆叠在一起。

#include <stdio.h>
main ()
{
    int n, i, age, hypo=0, mild=0, normal=0, mildhigh=0, modhigh=0, hyper=0, sp, dp ;
    printf("Number of patients = ");
    scanf("%d", &n);
    for (i=1; i<n+1; i++)
    {
        printf("What is the age? = \n");
        scanf("%d", &age);
        printf("Diastolic Pressure =\n");
        scanf("%d", &dp);
        printf("Systolic Pressure = \n");
        scanf("%d", &sp);
            if (dp>=35 && dp<=59 && sp>=50 && sp<=89)
            {
                    printf("\n                 Analysis  of Health Status                   \n");
                    printf("Age          DP in mmHg      SP in mmHg          Health Status   \n");
                    printf ("%d               %d            %d                 Hypotension\n", age, dp, sp);
                    hypo++;
            }
            else if (dp>=60 && dp<=69 && sp>=90 && sp<=99)
            {       
                    printf("\n                   Analysis  of Health Status                   \n"); 
                    printf("Age          DP in mmHg      SP in mmHg          Health Status   \n");
                    printf ("%d               %d            %d                 Mild Low\n", age, dp, sp);
                    mild++; 
            }
            else if (dp>=70 && dp<=84 && sp>=100 && sp<=129)
            {
                    printf("\n                   Analysis  of Health Status                   \n");
                    printf("Age          DP in mmHg      SP in mmHg          Health Status   \n");
                    printf ("%d               %d            %d                  Normal\n", age, dp, sp);
                    normal++;
            }

            else if (dp>=85 && dp<=89 && sp>=130 && sp<=139)
            {       
                    printf("\n                  Analysis  of Health Status                   \n");
                    printf("Age          DP in mmHg      SP in mmHg          Health Status   \n");
                    printf ("%d              %d              %d                 Stage 1\n", age, dp, sp);
                    mildhigh++; 
            }
            else if (dp>=90 && dp<=109 && sp>=140 && sp<=159)
            {       
                    printf("\n                   Analysis  of Health Status                   \n");
                    printf("Age          DP in mmHg      SP in mmHg          Health Status   \n");
                    printf ("%d               %d               %d               Stage 2\n", age, dp, sp);
                    modhigh++;
            }
            else if (dp>=110 && dp<=135 && sp>=160 && sp<=230)
            {       
                    printf("\n                   Analysis  of Health Status                   \n");
                    printf("Age          DP in mmHg      SP in mmHg          Health Status   \n");
                    printf ("%d              %d               %d               Stage 3\n", age, dp, sp);
                    hyper++;
            }
            else
            {       printf("Invalid input");
            }





            printf("\n      Summary of Health Status            \n");
            printf("Health Status                                      Number\n",mildhigh);
            printf("Hypotension                                           %d\n", hypo);
            printf("Mildlow                                               %d\n",mild);
            printf("Normal                                                %d\n",normal);
            printf("Mild High (Stage 1)                                   %d\n",mildhigh);
            printf("Moderately High BP (Stage 2)                          %d\n",modhigh);   
            printf("Hypertension/High BP (Stage 3)                        %d\n",hyper);

    }
}

【问题讨论】:

  • 那你在问什么?请更新您的帖子,详细说明堆叠输出的含义
  • 请修正缩进。 scanf 和 if-else 应该在同一级别
  • 打印中有代码重复 - 这很容易出错 - 考虑为轻度/重度/stage3 等添加一些静态字符串
  • Only Need One printf for Multiple Lines——只是一个使用例子。见int menu (void)函数。
  • 这是一个针对结构问题的大型程序。也许使用一两个变量将其简化为本质并重新询问。我怀疑您想使用数组来存储每个患者的值并写入所有输出而无需干预 scanf。请澄清。

标签: c loops for-loop


【解决方案1】:

看起来您在 for 循环中打印了摘要。移动到}的另一边

也叫

    printf("Health Status                 Number\n",mildhigh);

不要带额外的参数,因为那里没有%d

正如在 cmets 中所写的那样,修复缩进,因为它使您更容易阅读代码,将所有打印块分成一组内部和一组 for 循环,并为不同的小东西使用变量。

【讨论】:

    【解决方案2】:

    健康状况摘要应该在执行结束时只打印 1 次。因此,在for 循环结束后应该只有一次。

    #include <stdio.h>
    
    int main ()
    {
        int n, i, age, hypo=0, mild=0, normal=0, mildhigh=0, modhigh=0, hyper=0, sp, dp ;
        printf("Number of patients = ");
        scanf("%d", &n);
    
        for (i = 1; i < n + 1; i++)
        {
            printf("What is the age? = \n");
            scanf("%d", &age);
            printf("Diastolic Pressure =\n");
            scanf("%d", &dp);
            printf("Systolic Pressure = \n");
            scanf("%d", &sp);
    
            if (dp>=35 && dp<=59 && sp>=50 && sp<=89)
            {
                printf("\n                 Analysis  of Health Status                   \n");
                printf("Age          DP in mmHg      SP in mmHg          Health Status   \n");
                printf ("%d               %d            %d                 Hypotension\n", age, dp, sp);
                hypo++;
            }
            else if (dp>=60 && dp<=69 && sp>=90 && sp<=99)
            {       
                printf("\n                   Analysis  of Health Status                   \n"); 
                printf("Age          DP in mmHg      SP in mmHg          Health Status   \n");
                printf ("%d               %d            %d                 Mild Low\n", age, dp, sp);
                mild++; 
            }
            else if (dp>=70 && dp<=84 && sp>=100 && sp<=129)
            {
                printf("\n                   Analysis  of Health Status                   \n");
                printf("Age          DP in mmHg      SP in mmHg          Health Status   \n");
                printf ("%d               %d            %d                  Normal\n", age, dp, sp);
                normal++;
            }
    
            else if (dp>=85 && dp<=89 && sp>=130 && sp<=139)
            {       
                printf("\n                  Analysis  of Health Status                   \n");
                printf("Age          DP in mmHg      SP in mmHg          Health Status   \n");
                printf ("%d              %d              %d                 Stage 1\n", age, dp, sp);
                mildhigh++; 
            }
            else if (dp>=90 && dp<=109 && sp>=140 && sp<=159)
            {       
                printf("\n                   Analysis  of Health Status                   \n");
                printf("Age          DP in mmHg      SP in mmHg          Health Status   \n");
                printf ("%d               %d               %d               Stage 2\n", age, dp, sp);
                modhigh++;
            }
            else if (dp>=110 && dp<=135 && sp>=160 && sp<=230)
            {       
                printf("\n                   Analysis  of Health Status                   \n");
                printf("Age          DP in mmHg      SP in mmHg          Health Status   \n");
                printf ("%d              %d               %d               Stage 3\n", age, dp, sp);
                hyper++;
            }
            else
            {
                printf("Invalid input");
            }
    
    
        }
    
        printf("\n      Summary of Health Status            \n");
        printf("Health Status                                      Number\n",mildhigh);
        printf("Hypotension                                           %d\n", hypo);
        printf("Mildlow                                               %d\n",mild);
        printf("Normal                                                %d\n",normal);
        printf("Mild High (Stage 1)                                   %d\n",mildhigh);
        printf("Moderately High BP (Stage 2)                          %d\n",modhigh);   
        printf("Hypertension/High BP (Stage 3)                        %d\n",hyper);
    
        return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-04-15
      • 2012-12-11
      • 2013-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-08
      相关资源
      最近更新 更多