【问题标题】:Change multiple if to a loop?将多个if更改为循环?
【发布时间】:2015-12-22 12:29:19
【问题描述】:

我刚刚学会了如何使用循环,但我不知道如何翻译这段代码:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    float manyTest, gr1, gr2, gr3, gr4, gr5, gr6, gr7, gr8, gr9, gr10, gr11, gr12, gr13, gr14, gr15, gr16, gr17, gr18, gr19, gr20, gr21, gr22, gr23, gr24, gr25;
    manyTest = gr1 = gr2 = gr3 = gr4 = gr5 = gr6 = gr7 = gr8 = gr9 = gr10 = gr11 = gr12 = gr13 = gr14 = gr15 = gr16 = gr17 = gr18 = gr19 = gr20 = gr21 = gr22 = gr23 = gr24 = gr25 = 0;

    printf("How many tests you wanna average? (Minimum 1 Maximum 25)\n");
    scanf(" %f", &manyTest);
    if (manyTest <= 0) {
        printf("The Minimum is 1!\n");

    }
    else if (manyTest > 25){
        printf("The Maximum is 25!\n");

    }
    else {
        if (manyTest == 1) {
            printf("Write down your grades on those tests:\n");
            scanf(" %f", &gr1);
        }
        else if (manyTest == 2) {
            printf("Write down your grades on those tests:\n");
            scanf(" %f", &gr1);
            scanf(" %f", &gr2);
        }
        else if (manyTest == 3) {
            printf("Write down your grades on those tests:\n");
            scanf(" %f", &gr1);
            scanf(" %f", &gr2);
            scanf(" %f", &gr3);

        }
        else if (manyTest == 4) {
            printf("Write down your grades on those tests:\n");
            scanf(" %f", &gr1);
            scanf(" %f", &gr2);
            scanf(" %f", &gr2);
            scanf(" %f", &gr3);
            scanf(" %f", &gr4);

        }
        else if (manyTest == 5) {
            printf("Write down your grades on those tests:\n");
            scanf(" %f", &gr1);
            scanf(" %f", &gr2);
            scanf(" %f", &gr2);
            scanf(" %f", &gr3);
            scanf(" %f", &gr4);
            scanf(" %f", &gr5);
        }
        else if (manyTest == 6) {
            printf("Write down your grades on those tests:\n");
            scanf(" %f", &gr1);
            scanf(" %f", &gr2);
            scanf(" %f", &gr2);
            scanf(" %f", &gr3);
            scanf(" %f", &gr4);
            scanf(" %f", &gr5);
            scanf(" %f", &gr6);

        }
        else if (manyTest == 7) {
            printf("Write down your grades on those tests:\n");
            scanf(" %f", &gr1);
            scanf(" %f", &gr2);
            scanf(" %f", &gr2);
            scanf(" %f", &gr3);
            scanf(" %f", &gr4);
            scanf(" %f", &gr5);
            scanf(" %f", &gr6);
            scanf(" %f", &gr7);
        }
        else if (manyTest == 8) {
            printf("Write down your grades on those tests:\n");
            scanf(" %f", &gr1);
            scanf(" %f", &gr2);
            scanf(" %f", &gr2);
            scanf(" %f", &gr4);
            scanf(" %f", &gr5);
            scanf(" %f", &gr6);
            scanf(" %f", &gr7);
            scanf(" %f", &gr8);
        }
        else if (manyTest == 9) {
            printf("Write down your grades on those tests:\n");
            scanf(" %f", &gr1);
            scanf(" %f", &gr2);
            scanf(" %f", &gr3);
            scanf(" %f", &gr4);
            scanf(" %f", &gr5);
            scanf(" %f", &gr6);
            scanf(" %f", &gr7);
            scanf(" %f", &gr8);
            scanf(" %f", &gr9);
        }
        else if (manyTest == 10) {
            printf("Write down your grades on those tests:\n");
            scanf(" %f", &gr1);
            scanf(" %f", &gr2);
            scanf(" %f", &gr3);
            scanf(" %f", &gr4);
            scanf(" %f", &gr5);
            scanf(" %f", &gr6);
            scanf(" %f", &gr7);
            scanf(" %f", &gr8);
            scanf(" %f", &gr9);
            scanf(" %f", &gr10);
        }
        else if (manyTest == 11) {
            printf("Write down your grades on those tests:\n");
            scanf(" %f", &gr1);
            scanf(" %f", &gr2);
            scanf(" %f", &gr3);
            scanf(" %f", &gr4);
            scanf(" %f", &gr5);
            scanf(" %f", &gr6);
            scanf(" %f", &gr7);
            scanf(" %f", &gr8);
            scanf(" %f", &gr9);
            scanf(" %f", &gr10);
            scanf(" %f", &gr11);
        }
        else if (manyTest == 12) {
            printf("Write down your grades on those tests:\n");
            scanf(" %f", &gr1);
            scanf(" %f", &gr2);
            scanf(" %f", &gr3);
            scanf(" %f", &gr4);
            scanf(" %f", &gr5);
            scanf(" %f", &gr6);
            scanf(" %f", &gr7);
            scanf(" %f", &gr8);
            scanf(" %f", &gr9);
            scanf(" %f", &gr10);
            scanf(" %f", &gr11);
            scanf(" %f", &gr12);
        }
        else if (manyTest == 13) {
            printf("Write down your grades on those tests:\n");
            scanf(" %f", &gr1);
            scanf(" %f", &gr2);
            scanf(" %f", &gr3);
            scanf(" %f", &gr4);
            scanf(" %f", &gr5);
            scanf(" %f", &gr6);
            scanf(" %f", &gr7);
            scanf(" %f", &gr8);
            scanf(" %f", &gr9);
            scanf(" %f", &gr10);
            scanf(" %f", &gr11);
            scanf(" %f", &gr12);
            scanf(" %f", &gr13);
        }
        else if (manyTest == 14) {
            printf("Write down your grades on those tests:\n");
            scanf(" %f", &gr1);
            scanf(" %f", &gr2);
            scanf(" %f", &gr3);
            scanf(" %f", &gr4);
            scanf(" %f", &gr5);
            scanf(" %f", &gr6);
            scanf(" %f", &gr7);
            scanf(" %f", &gr8);
            scanf(" %f", &gr9);
            scanf(" %f", &gr10);
            scanf(" %f", &gr11);
            scanf(" %f", &gr12);
            scanf(" %f", &gr13);
            scanf(" %f", &gr14);
        }
        else if (manyTest == 15) {
            printf("Write down your grades on those tests:\n");
            scanf(" %f", &gr1);
            scanf(" %f", &gr2);
            scanf(" %f", &gr3);
            scanf(" %f", &gr4);
            scanf(" %f", &gr5);
            scanf(" %f", &gr6);
            scanf(" %f", &gr7);
            scanf(" %f", &gr8);
            scanf(" %f", &gr9);
            scanf(" %f", &gr10);
            scanf(" %f", &gr11);
            scanf(" %f", &gr12);
            scanf(" %f", &gr13);
            scanf(" %f", &gr14);
            scanf(" %f", &gr15);
        }
        else if (manyTest == 16) {
            printf("Write down your grades on those tests:\n");
            scanf(" %f", &gr1);
            scanf(" %f", &gr2);
            scanf(" %f", &gr3);
            scanf(" %f", &gr4);
            scanf(" %f", &gr5);
            scanf(" %f", &gr6);
            scanf(" %f", &gr7);
            scanf(" %f", &gr8);
            scanf(" %f", &gr9);
            scanf(" %f", &gr10);
            scanf(" %f", &gr11);
            scanf(" %f", &gr12);
            scanf(" %f", &gr13);
            scanf(" %f", &gr14);
            scanf(" %f", &gr15);
            scanf(" %f", &gr16);
        }
        else if (manyTest == 17) {
            printf("Write down your grades on those tests:\n");
            scanf(" %f", &gr1);
            scanf(" %f", &gr2);
            scanf(" %f", &gr3);
            scanf(" %f", &gr4);
            scanf(" %f", &gr5);
            scanf(" %f", &gr6);
            scanf(" %f", &gr7);
            scanf(" %f", &gr8);
            scanf(" %f", &gr9);
            scanf(" %f", &gr10);
            scanf(" %f", &gr11);
            scanf(" %f", &gr12);
            scanf(" %f", &gr13);
            scanf(" %f", &gr14);
            scanf(" %f", &gr15);
            scanf(" %f", &gr16);
            scanf(" %f", &gr17);
        }
        else if (manyTest == 18) {
            printf("Write down your grades on those tests:\n");
            scanf(" %f", &gr1);
            scanf(" %f", &gr2);
            scanf(" %f", &gr3);
            scanf(" %f", &gr4);
            scanf(" %f", &gr5);
            scanf(" %f", &gr6);
            scanf(" %f", &gr7);
            scanf(" %f", &gr8);
            scanf(" %f", &gr9);
            scanf(" %f", &gr10);
            scanf(" %f", &gr11);
            scanf(" %f", &gr12);
            scanf(" %f", &gr13);
            scanf(" %f", &gr14);
            scanf(" %f", &gr15);
            scanf(" %f", &gr16);
            scanf(" %f", &gr17);
            scanf(" %f", &gr18);
        }
        else if (manyTest == 19) {
            printf("Write down your grades on those tests:\n");
            scanf(" %f", &gr1);
            scanf(" %f", &gr2);
            scanf(" %f", &gr3);
            scanf(" %f", &gr4);
            scanf(" %f", &gr5);
            scanf(" %f", &gr6);
            scanf(" %f", &gr7);
            scanf(" %f", &gr8);
            scanf(" %f", &gr9);
            scanf(" %f", &gr10);
            scanf(" %f", &gr11);
            scanf(" %f", &gr12);
            scanf(" %f", &gr13);
            scanf(" %f", &gr14);
            scanf(" %f", &gr15);
            scanf(" %f", &gr16);
            scanf(" %f", &gr17);
            scanf(" %f", &gr18);
            scanf(" %f", &gr19);
        }
        else if (manyTest == 20) {
            printf("Write down your grades on those tests:\n");
            scanf(" %f", &gr1);
            scanf(" %f", &gr2);
            scanf(" %f", &gr3);
            scanf(" %f", &gr4);
            scanf(" %f", &gr5);
            scanf(" %f", &gr6);
            scanf(" %f", &gr7);
            scanf(" %f", &gr8);
            scanf(" %f", &gr9);
            scanf(" %f", &gr10);
            scanf(" %f", &gr11);
            scanf(" %f", &gr12);
            scanf(" %f", &gr13);
            scanf(" %f", &gr14);
            scanf(" %f", &gr15);
            scanf(" %f", &gr16);
            scanf(" %f", &gr17);
            scanf(" %f", &gr18);
            scanf(" %f", &gr19);
            scanf(" %f", &gr20);
        }
        else if (manyTest == 21) {
            printf("Write down your grades on those tests:\n");
            scanf(" %f", &gr1);
            scanf(" %f", &gr2);
            scanf(" %f", &gr3);
            scanf(" %f", &gr4);
            scanf(" %f", &gr5);
            scanf(" %f", &gr6);
            scanf(" %f", &gr7);
            scanf(" %f", &gr8);
            scanf(" %f", &gr9);
            scanf(" %f", &gr10);
            scanf(" %f", &gr11);
            scanf(" %f", &gr12);
            scanf(" %f", &gr13);
            scanf(" %f", &gr14);
            scanf(" %f", &gr15);
            scanf(" %f", &gr16);
            scanf(" %f", &gr17);
            scanf(" %f", &gr18);
            scanf(" %f", &gr19);
            scanf(" %f", &gr20);
            scanf(" %f", &gr21);
        }
        else if (manyTest == 22) {
            printf("Write down your grades on those tests:\n");
            scanf(" %f", &gr1);
            scanf(" %f", &gr2);
            scanf(" %f", &gr3);
            scanf(" %f", &gr4);
            scanf(" %f", &gr5);
            scanf(" %f", &gr6);
            scanf(" %f", &gr7);
            scanf(" %f", &gr8);
            scanf(" %f", &gr9);
            scanf(" %f", &gr10);
            scanf(" %f", &gr11);
            scanf(" %f", &gr12);
            scanf(" %f", &gr13);
            scanf(" %f", &gr14);
            scanf(" %f", &gr15);
            scanf(" %f", &gr16);
            scanf(" %f", &gr17);
            scanf(" %f", &gr18);
            scanf(" %f", &gr19);
            scanf(" %f", &gr20);
            scanf(" %f", &gr21);
            scanf(" %f", &gr22);
        }
        else if (manyTest == 23) {
            printf("Write down your grades on those tests:\n");
            scanf(" %f", &gr1);
            scanf(" %f", &gr2);
            scanf(" %f", &gr3);
            scanf(" %f", &gr4);
            scanf(" %f", &gr5);
            scanf(" %f", &gr6);
            scanf(" %f", &gr7);
            scanf(" %f", &gr8);
            scanf(" %f", &gr9);
            scanf(" %f", &gr10);
            scanf(" %f", &gr11);
            scanf(" %f", &gr12);
            scanf(" %f", &gr13);
            scanf(" %f", &gr14);
            scanf(" %f", &gr15);
            scanf(" %f", &gr16);
            scanf(" %f", &gr17);
            scanf(" %f", &gr18);
            scanf(" %f", &gr19);
            scanf(" %f", &gr20);
            scanf(" %f", &gr21);
            scanf(" %f", &gr22);
            scanf(" %f", &gr23);
        }
        else if (manyTest == 24) {
            printf("Write down your grades on those tests:\n");
            scanf(" %f", &gr1);
            scanf(" %f", &gr2);
            scanf(" %f", &gr3);
            scanf(" %f", &gr4);
            scanf(" %f", &gr5);
            scanf(" %f", &gr6);
            scanf(" %f", &gr7);
            scanf(" %f", &gr8);
            scanf(" %f", &gr9);
            scanf(" %f", &gr10);
            scanf(" %f", &gr11);
            scanf(" %f", &gr12);
            scanf(" %f", &gr13);
            scanf(" %f", &gr14);
            scanf(" %f", &gr15);
            scanf(" %f", &gr16);
            scanf(" %f", &gr17);
            scanf(" %f", &gr18);
            scanf(" %f", &gr19);
            scanf(" %f", &gr20);
            scanf(" %f", &gr21);
            scanf(" %f", &gr22);
            scanf(" %f", &gr23);
            scanf(" %f", &gr24);
        }
        else if (manyTest == 25) {
            printf("Write down your grades on those tests:\n");
            scanf(" %f", &gr1);
            scanf(" %f", &gr2);
            scanf(" %f", &gr3);
            scanf(" %f", &gr4);
            scanf(" %f", &gr5);
            scanf(" %f", &gr6);
            scanf(" %f", &gr7);
            scanf(" %f", &gr8);
            scanf(" %f", &gr9);
            scanf(" %f", &gr10);
            scanf(" %f", &gr11);
            scanf(" %f", &gr12);
            scanf(" %f", &gr13);
            scanf(" %f", &gr14);
            scanf(" %f", &gr15);
            scanf(" %f", &gr16);
            scanf(" %f", &gr17);
            scanf(" %f", &gr18);
            scanf(" %f", &gr19);
            scanf(" %f", &gr20);
            scanf(" %f", &gr21);
            scanf(" %f", &gr22);
            scanf(" %f", &gr23);
            scanf(" %f", &gr24);
            scanf(" %f", &gr25);
        }
        float avg = (gr1 + gr2 + gr3 + gr4 + gr5 + gr6 + gr7 + gr8 + gr9 + gr10 + gr11 + gr12 + gr13 + gr14 + gr15 + gr16 + gr17 + gr18 + gr19 + gr20 + gr21 + gr22 + gr23 + gr24 + gr25) / manyTest;
        printf("Your average grade is: %.2f\n", avg);

    }

    system("pause");
    return 0;

}

从所有这些if...if...if...if....if 到一些单个循环。如您所见,我的代码很长,而且是在我学会如何使用循环之前完成的。所以,现在我学会了我不知道该怎么做。

【问题讨论】:

  • 使用循环和数组。
  • 你需要使用数组而不是你的变量gr1, gr2
  • 你不能循环不同的变量,但是你可以循环一个数组。
  • 如果最大值为 100,你将如何编写该代码?
  • 你用scanf()错了很多次!为什么不使用数组?是禁止的吗?

标签: c loops if-statement


【解决方案1】:

您需要的只是一个循环用于您的manyTest 值,您根本不需要数组

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    float manyTest = 0, gr = 0, avg = 0; //firstly, replace many gr with single gr, avg for averaging
    int i; //just indexer

    printf("How many tests you wanna average? (Minimum 1 Maximum 25)\n");
    scanf(" %f", &manyTest);
    if (manyTest <= 0) {
        printf("The Minimum is 1!\n");
    }
    else if (manyTest > 25){
        printf("The Maximum is 25!\n");
    }
    else {
        printf("Write down your grades on those tests:\n");
        for (i = 0; i < manyTest; ++i) {
            scanf(" %f", &gr); //use gr every time
            avg += gr; //then immediately add to avg!
        }
        printf("Your average grade is: %.2f\n", avg / manyTest); //notice the division by manyTest
    }

    system("pause");
    return 0;
}

完成!

示例结果:

【讨论】:

    【解决方案2】:

    这是一个相对稳健且完整的示例,它不限制输入的总量。相反,您发送 EOF 来结束输入流:

    #include <errno.h>
    #include <stdio.h>
    
    int main() {
      int total = 0;
      double acc = 0, val;
    
      for (;;) {
        fputs("Value: ", stdout);
        fflush(stdout);
    
        errno = 0;
        int res = fscanf(stdin, "%lf", &val);
    
        if (res == EOF) {
          // Handle successful end of input or read error.
          if (errno == 0) {
            fprintf(stdout, "Done! You entered %d values averaging %f.\n",
                    total, acc / total);
          } else {
            fputs("There was an error, aborting!\n", stdout);
          }
          break;
        } else if (res == 0) {
          // Handle parse error.
          fputs("Sorry, I did not understand. Try again.\n", stdout);
          clearerr(stdin);
          for (int r = 0; r != EOF && r != '\n'; r = fgetc(stdin)) {}
        } else {
          // Handle successful input.
          acc += val;
          ++total;
        }
      }
    }
    

    Demo

    【讨论】:

    • 我希望有足够的错误来确保复制/粘贴它会得到一个糟糕的成绩:)
    【解决方案3】:

    有一种方法可以计算一组数字的平均值,而无需将每个数字单独存储在数组中或单独命名变量(yuk)中。

    只保留一个累计,最后除以。

    【讨论】:

      【解决方案4】:

      您可以使用数组,然后迭代以获取输入。

      这样的事情是可以做到的——

      int n;                          // number of tests
      scanf("%d",&n);                 // take input number of tests 
      int grades[n];                  // declare your array 
      for(int i=0;i<n;i++){           // loop for n times
         scanf(" %d",&grades[i]);     // take input n number of grades 
      }
      

      【讨论】:

      • 别忘了跳过换行符。
      • 问题不在于减少 if..else 吗?
      • @Bathsheba 我从来没有说过这很糟糕,只是优先级,你知道的。 :)
      • @Bathsheba 先生,我并没有真正理解你的第一条评论。
      • 我认为你在for 循环中需要scanf(" %。注意空格
      【解决方案5】:

      要回答第一部分,关于减少if...else 条件检查,您不能全部从代码中删除if..else 条件,但是,您可以肯定地减少else 部分。为此,您需要使用switch statement。类似的东西

      if (manyTest <= 0) {
          printf("The Minimum is 1!\n");
      
      }
      else if (manyTest > 25){
          printf("The Maximum is 25!\n");
      
      }
      else {
          switch(manyTest)
          {
      
             case 1: //.... break;
             case 2: //..... break;
          }
      

      可以帮到你。

      然后,为了减少代码中使用的变量数量,您可以使用array,并使用for 循环迭代一定次数以填充所需数量的输入。

      【讨论】:

      • 谢谢,但我不知道如何使用 switch 语句。你能解释一下怎么做吗?或者你说我应该继续学习直到我能够使用它?
      • @kuyy 那么是时候学习了。我已经用一个简单教程的链接更新了我的答案。希望对您有所帮助。
      【解决方案6】:

      您可以使用简单的数组作为成绩输入和平均值,这对您来说只是领先一步

      int main()
      {
          int manyTest = 0;
          float gr[25];
      
          printf("How many tests you wanna average? (Minimum 1 Maximum 25)\n");
          scanf(" %f", &manyTest);
          if (manyTest <= 0) {
              printf("The Minimum is 1!\n");
      
          }
          else if (manyTest > 25){
              printf("The Maximum is 25!\n");
      
          }
          for(int i=0;i<manyTest;i++){
              printf("Write down your grades for test %d:",i);
               scanf(" %f", &gr[i]);
          }
      //Average
          float avg;
          for(int j=0; j<manyTest;j++){
              avg+=gr[j];
          }
          avg=(avg/manyTest);
          printf("Your average grade is: %.2f\n", avg);
      }
      

      【讨论】:

      • 好像不太好用。它只是一直问我“blalahlahl 考试成绩 number
      • grade for test 在 for 循环中,它的运行时间与 manyTest 的输入完全相同
      猜你喜欢
      • 2021-11-23
      • 1970-01-01
      • 1970-01-01
      • 2012-01-03
      • 2018-08-13
      • 2017-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多