【发布时间】: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' && loop != 3)这不合逻辑。loop是计数器还是存储用户答案的变量?选择。 -
if(loop != 'y' && loop != 3) loop='n';不应该在 for 循环之外吗? -
scanf( "%s/n", &teacher);是一个有趣的问题,但不是问题。 (删除/n)。使用scanf( "%29s", teacher); -
看起来 return 语句在 while 循环内,而 if 语句应该在 for 循环外。编辑:根据您对问题的描述,听起来您只需要for循环,而while循环不是必需的。 'loop != 3' 应该是 'i != 3'
标签: c loops if-statement while-loop