C语言 在 while 循环中的 if 语句无法执行并退出程序



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

      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 Frequentlyn");
      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: %dn",num1);
      printf("How often does the teacher explain material clearly : %dn",num2);
      printf("How often is the teacher available outside of class : %dn",num3);
      printf("How often does the teacher provide helpful comments on papers and exams: %dn",num4);
      printf("How often does the teacher stimulate interest in material: %dn",num5);
      printf("How often does the teacher adjust the pace of class to the students' level of understanding: %dn",num6);
      printf("How often does the teacher effectively encourage students to ask questions and give answers: %dn",num7);
      printf("How is the teacher tolerant of different opinions expressed in class: %dn",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;
  }
}

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

在你的代码中,你有两个循环,只要小心一点,你应该能够把它简化为其中一个。特别是,看起来您的returnwhile循环内,这意味着它是......很困惑。弄清楚你的循环应该做什么,然后再试一次。

if 语句

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

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

您无缘无故地使用了 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 Frequentlyn");
      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: %dn",num1);
      printf("How often does the teacher explain material clearly : %dn",num2);
      printf("How often is the teacher available outside of class : %dn",num3);
      printf("How often does the teacher provide helpful comments on papers and exams: %dn",num4);
      printf("How often does the teacher stimulate interest in material: %dn",num5);
      printf("How often does the teacher adjust the pace of class to the students' level of understanding: %dn",num6);
      printf("How often does the teacher effectively encourage students to ask questions and give answers: %dn",num7);
      printf("How is the teacher tolerant of different opinions expressed in class: %dn",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;
}

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

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 ***
  }
}

最新更新