【问题标题】:If statement inside of a while loop that fails to execute and exit program无法执行并退出程序的while循环内的if语句
【发布时间】:2017-05-08 19:22:56
【问题描述】:

所以我的主要问题是这段代码的后半部分,尤其是这个:

      printf("Do you want to evaluate another teacher? (y/n) : ");
      printf("\n");
      scanf(" %c", &loop);
      if(loop != 'y' && loop != 3)
        loop='n';

我正在创建一个进行调查的程序。但是,学生最多只能进行 3 次调查,并且在每次调查结束时都会询问他们是否愿意进行另一次调查。我的问题是在他们进行第三次调查后出现的。之后,调查提示Do you want to evaluate another teacher? (y/n) 的相同问题,如果学生回答y,则代码循环返回并让他们进行另一次调查,而不是听我的条件,即在三个调查后程序应该自动结束。如果他们回答n,它仍然会重新进入循环!

对于如何让我的这部分代码与我的其他代码和谐共存并正常工作,我感到非常困惑。非常感谢您的帮助!

如果您愿意,这是我的全部代码:

#include <stdio.h>

int main()
{
  int i = 0;
  char loop='y';


  while(loop == 'y' ){
    for(i = 0; i<4; i++){

      int num1,num2,num3,num4,num5,num6,num7,num8;
      int result;
      int input;
      char name[30];
      char teacher[30];

     printf("Enter your name : ");
      scanf("%s", &name);
      printf("\n");
      printf("Which teacher do you want to evaluate : ");
      scanf( "%s/n", &teacher);
      printf("\n");
      printf("Answer with 1 for Never upto 7 for Frequently\n");
      printf("\n");
      printf("How often does the teacher indicate where the class is going? \n ");
      scanf("%d",&num1);
      printf("How often does the teacher explain material clearly? \n ");
      scanf("%d",&num2);
      printf("How often is the teacher available outside of class? \n ");
      scanf("%d",&num3);
      printf("How often does the teacher provide helpful comments on papers and exams? \n ");
      scanf("%d",&num4);
      printf("How often does the teacher stimulate interest in material? \n ");
      scanf("%d",&num5);
      printf("How often does the teacher adjust the pace of class to the students' level of understanding? \n ");
      scanf("%d",&num6);
      printf("How often does the teacher effectively encourage students to ask questions and give answers? \n ");
      scanf("%d",&num7);
      printf("How is the teacher tolerant of different opinions expressed in class? \n ");
      scanf("%d",&num8);

      printf("******************************************************************************\n");
      printf("******************************************************************************\n");
      printf("Student's name : %s.\n", name);
      printf("Teacher's name : %s.\n", teacher);
      printf("How often does the teacher indicate where the class is going: %d\n",num1);
      printf("How often does the teacher explain material clearly : %d\n",num2);
      printf("How often is the teacher available outside of class : %d\n",num3);
      printf("How often does the teacher provide helpful comments on papers and exams: %d\n",num4);
      printf("How often does the teacher stimulate interest in material: %d\n",num5);
      printf("How often does the teacher adjust the pace of class to the students' level of understanding: %d\n",num6);
      printf("How often does the teacher effectively encourage students to ask questions and give answers: %d\n",num7);
      printf("How is the teacher tolerant of different opinions expressed in class: %d\n",num8);
      printf("******************************************************************************\n");
      printf("******************************************************************************\n");

      printf("Do you want to evaluate another teacher? (y/n) : ");
      printf("\n");
      scanf(" %c", &loop);
      if(loop != 'y' && loop != 3)
        loop='n';
    }
    return 0;
  }
}

【问题讨论】:

  • if(loop != 'y' &amp;&amp; loop != 3) 这不合逻辑。 loop 是计数器还是存储用户答案的​​变量?选择。
  • if(loop != 'y' &amp;&amp; loop != 3) loop='n'; 不应该在 for 循环之外吗?
  • scanf( "%s/n", &amp;teacher); 是一个有趣的问题,但不是问题。 (删除/n)。使用scanf( "%29s", teacher);
  • 看起来 return 语句在 while 循环内,而 if 语句应该在 for 循环外。编辑:根据您对问题的描述,听起来您只需要for循环,而while循环不是必需的。 'loop != 3' 应该是 'i != 3'

标签: c loops if-statement while-loop


【解决方案1】:

你真的需要while 和 for 循环吗? for 循环在做什么?

在您的代码中,您有两个循环,稍加注意,您应该能够将其归结为其中一个。特别是,看起来您的 return 在 while 循环内,这意味着它......非常混乱。弄清楚你的循环应该做什么,然后再试一次。

【讨论】:

    【解决方案2】:

    if 语句

    if(loop != 'y' && loop != 3)
            loop='n';
    

    应该在 for 循环之外。在任何情况下,您都不需要两个循环。您应该将其更改为仅使用一个循环。而这个if 语句应该是循环结束前的最后一条语句。

    【讨论】:

      【解决方案3】:

      你无缘无故地使用了2个循环,而且你每次都询问用户的名字......

      你应该这样做:

      #include <stdio.h>
      
      
      int main()
      {
        int i = 0;
        char loop;
      
      
        int count=0;  //new variable to know how many times he filled the survey
      
        //You dont need to ask about your name everytime...
        char name[30];
        printf("Enter your name : ");
        scanf("%s", &name);
        printf("\n");
      
      
        do{
      
      
            int num1,num2,num3,num4,num5,num6,num7,num8;
            int result;
            int input;
            char teacher[30];
      
      
            printf("Which teacher do you want to evaluate : ");
            scanf( "%s/n", &teacher);
            printf("\n");
            printf("Answer with 1 for Never upto 7 for Frequently\n");
            printf("\n");
            printf("How often does the teacher indicate where the class is going? \n ");
            scanf("%d",&num1);
            printf("How often does the teacher explain material clearly? \n ");
            scanf("%d",&num2);
            printf("How often is the teacher available outside of class? \n ");
            scanf("%d",&num3);
            printf("How often does the teacher provide helpful comments on papers and exams? \n ");
            scanf("%d",&num4);
            printf("How often does the teacher stimulate interest in material? \n ");
            scanf("%d",&num5);
            printf("How often does the teacher adjust the pace of class to the students' level of understanding? \n ");
            scanf("%d",&num6);
            printf("How often does the teacher effectively encourage students to ask questions and give answers? \n ");
            scanf("%d",&num7);
            printf("How is the teacher tolerant of different opinions expressed in class? \n ");
            scanf("%d",&num8);
      
            printf("******************************************************************************\n");
            printf("******************************************************************************\n");
            printf("Student's name : %s.\n", name);
            printf("Teacher's name : %s.\n", teacher);
            printf("How often does the teacher indicate where the class is going: %d\n",num1);
            printf("How often does the teacher explain material clearly : %d\n",num2);
            printf("How often is the teacher available outside of class : %d\n",num3);
            printf("How often does the teacher provide helpful comments on papers and exams: %d\n",num4);
            printf("How often does the teacher stimulate interest in material: %d\n",num5);
            printf("How often does the teacher adjust the pace of class to the students' level of understanding: %d\n",num6);
            printf("How often does the teacher effectively encourage students to ask questions and give answers: %d\n",num7);
            printf("How is the teacher tolerant of different opinions expressed in class: %d\n",num8);
            printf("******************************************************************************\n");
            printf("******************************************************************************\n");
      
           count++; 
      
           if (count<3) {
            printf("Do you want to evaluate another teacher? (y/n) : ");
            printf("\n");
            scanf("%s", &loop);
           }
      
      
      
        } while (loop=='y' && count<3);
      
        return 0;
      }
      

      【讨论】:

        【解决方案4】:

        这是一个更正的版本。我只关注您所询问的内容,而不是您的代码可能存在的任何其他问题。

        int main() {
          int i;
          char loop;
        
          for(i = 0; i < 3; i++) {
            if(i != 0) { //After the first survey, prompt before we interview them
              printf("Do you want to evaluate another teacher? (y/n) : \n");
              scanf(" %c", &loop);
              if(loop != 'y')
                break; //Quit the loop
            }
            //*** Survey prompts here ***
          }
        }
        

        【讨论】:

        • 这实际上是一个 do...while 循环可能是更好的选择的情况。以这种方式使用 if,关于迭代变量 i 的含义的特殊知识在两个不同的地方,而它可能只在一个地方,这是不幸的。如果有效迭代次数后来变为 5 而不是 3 怎么办?您必须在 i
        • @Craig 关于需要特殊知识的好点,但我仍然不确定 do...while 循环是最好的。看看我的编辑。
        猜你喜欢
        • 2017-02-02
        • 1970-01-01
        • 2023-04-05
        • 2015-09-26
        • 2021-04-25
        • 2015-05-19
        • 1970-01-01
        • 2019-12-16
        • 1970-01-01
        相关资源
        最近更新 更多