C while循环中的If语句,该语句无法执行并退出程序
所以我的主要问题是这段代码的后半部分,特别是:C while循环中的If语句,该语句无法执行并退出程序,c,loops,if-statement,while-loop,C,Loops,If Statement,While Loop,所以我的主要问题是这段代码的后半部分,特别是: printf("Do you want to evaluate another teacher? (y/n) : "); printf("\n"); scanf(" %c", &loop); if(loop != 'y' && loop != 3) loop='n'; 我正在创建一个接受调查的程序。但是,学生最多只能进行3次调查,每次调查结束时都会询问他们是否
printf("Do you want to evaluate another teacher? (y/n) : ");
printf("\n");
scanf(" %c", &loop);
if(loop != 'y' && loop != 3)
loop='n';
我正在创建一个接受调查的程序。但是,学生最多只能进行3次调查,每次调查结束时都会询问他们是否愿意进行另一次调查。我的问题发生在他们进行第三次调查之后。之后,调查会提出同样的问题:你想评价另一位老师吗?(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;
}
}
#包括
int main()
{
int i=0;
char-loop='y';
while(循环=='y'){
对于(i=0;i您真的需要while
和for
循环吗?for
循环做什么
在你的代码中,你有两个循环,只要稍加注意,你应该能够把它归结为其中一个。特别是,你的
返回
似乎在循环中,而循环意味着它…相当混乱。弄清楚你的循环应该做什么,然后再试一次。你真的需要这两个吗>而和for
循环?for
循环的在做什么
在您的代码中,您有两个循环,只要稍微小心,您应该能够将其归结为其中的一个。特别是,您的返回似乎在循环中,而循环意味着它…非常混乱。找出您的循环应该做什么,然后再试一次。if语句
if(loop != 'y' && loop != 3)
loop='n';
应该在for循环之外。在任何情况下,您都不需要两个循环。您应该将其更改为仅使用一个循环。此if
语句应该是该循环结束之前的最后一个语句。if语句
if(loop != 'y' && loop != 3)
loop='n';
应该在for循环之外。在任何情况下,您不需要两个循环。您应该将其更改为仅使用一个循环。此if
语句应该是该循环结束之前的最后一个语句。这里是一个更正版本。我只关注您询问的问题,而不是您的同事可能存在的任何其他问题德
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 ***
}
}
intmain(){
int i;
字符循环;
对于(i=0;i<3;i++){
如果(i!=0){//在第一次调查之后,在我们采访他们之前提示
printf(“您想评估另一位教师吗?(是/否):\n”);
scanf(“%c”、&loop);
如果(循环!=“y”)
break;//退出循环
}
//***此处有调查提示***
}
}
这是一个正确的版本。我只关注您所问的问题,而不是代码中可能存在的任何其他问题
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 ***
}
}
intmain(){
int i;
字符循环;
对于(i=0;i<3;i++){
如果(i!=0){//在第一次调查之后,在我们采访他们之前提示
printf(“您想评估另一位教师吗?(是/否):\n”);
scanf(“%c”、&loop);
如果(循环!=“y”)
break;//退出循环
}
//***此处有调查提示***
}
}
您无缘无故地使用了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;
}
#包括
int main()
{
int i=0;
字符循环;
int count=0;//新变量,用于了解他填写调查的次数
//你不必每次都问你的名字。。。
字符名[30];
printf(“输入您的姓名:”);
scanf(“%s”、&name);
printf(“\n”);
做{
int num1、num2、num3、num4、num5、num6、num7、num8;
int结果;
int输入;
教师[30];
printf(“你想评估哪位老师:”);
scanf(“%s/n”和“教师”);
printf(“\n”);
printf(“回答1表示从不,回答7表示频繁”);
printf(“\n”);
printf(“老师多长时间指出一次班级的去向?\n”);
scanf(“%d”&num1);
printf(“老师多久解释一次材料?\n”);
scanf(“%d”&num2);
printf(“老师多久一次课外有空?\n”);
scanf(“%d”和num3);
printf(“老师多久对论文和考试提供一次有用的评论?\n”);
scanf(“%d”&num4);
printf(“教师多久激发一次对材料的兴趣?\n”);
scanf(“%d”和&num5);
printf(“教师多久调整一次课堂节奏以适应学生的理解水平?\n”);
scanf(“%d”&num6);
printf(“教师多久有效地鼓励学生提问并给出答案?\n”);
scanf(“%d”&num7);
printf(“老师如何容忍课堂上表达的不同观点?\n”);
scanf(“%d”&num8);
printf(“******************************************************************************************************************************************************************\n”);
printf(“******************************************************************************************************************************************************************\n”);
printf(“学生姓名:%s.\n”,姓名);
printf(“教师姓名:%s.\n”,教师);
printf(“教师多长时间指出一次班级的去向:%d\n”,num1);
printf(“教师多长时间清楚地解释一次材料:%d\n”,num2);
printf(“教师在课外的时间间隔:%d\n”,num3);
printf(“教师多久对论文和考试提供一次有用的评论:%d\n”,num4);
printf(“教师多久激发一次对材料的兴趣:%d\n”,num5);
printf(“教师根据学生的理解水平调整课堂节奏的频率:%d\n”,num6);
printf(“教师多久有效地鼓励学生提问和提问一次?”