循环中完成多次输入输出 c语言 求教啊
int main()
{
char b;
float m,n,a,i,c,x;
for(i=0;i<=2;i++)
{
printf("Please enter a name of student:\n");
scanf("%c",&b);
printf("please enter a num of student:\n");
scanf("%d",&n);
printf("please enter three grades of him:\n");
scanf("%d,%d,%d",&m,&c,&x);
printf("the student`s ave grade is:%f\n",(m+c+x)/3);
}
return 0;
} 展开
#include<stdio.h>
int main()
{
char b;//字符
int i,n;//整形
float m,c,x;//浮点型
for(i=0;i<=2;i++)
{
printf("Please enter a name of student:\n");
fflush(stdin); //清键盘缓冲区
scanf("%c",&b);
printf("please enter a num of student:\n");
scanf("%d",&n);
printf("please enter three grades of him:\n");
scanf("%f%f%f",&m,&c,&x); //浮点输入
printf("the student`s ave grade is:%f\n",(m+c+x)/3);
}
return 0;
}
int main()
{
char b[20];
float m,i,c,x;/*这里将a去掉*/
int n;
for(i=0;i<=2;i++)
{
printf("Please enter a name of student:\n"); /*存储名字改用字符串*/
scanf("%s",&b);
printf("please enter a num of student:\n");
scanf("%d",&n); /*n是整型的*/
printf("please enter three grades of him:\n");
scanf("%f,%f,%f",&m,&c,&x); /*这三个存储的是float型的*/
printf("the student`s ave grade is:%f\n",(m+c+x)/3);
}
return 0;
}
参考一楼,但是
float m,a,i,c,x;
这里将a去掉,应为你这里定义了,后面没有用,没有初始化,所以会出错!
int main()
{
char b;
/**************你定义的是单精度**********************/
float m,n,a,i,c,x;
for(i=0;i<=2;i++)
{
printf("Please enter a name of student:\n");
scanf("%c",&b);
printf("please enter a num of student:\n");
scanf("%d",&n);
printf("please enter three grades of him:\n");
/***********************却是按照整型数据输入*********************/
scanf("%d,%d,%d",&m,&c,&x); //这样肯定会出问题,应该为scanf("%f,%f,%f",&m,&c,&x); *********这样就行了
printf("the student`s ave grade is:%f\n",(m+c+x)/3);
}
return 0;
}
int main()
{
char b[20];
float m,a,i,c,x;
int n;
for(i=0;i<=2;i++)
{
printf("Please enter a name of student:\n"); /*存储名字改用字符串*/
scanf("%s",&b);
printf("please enter a num of student:\n");
scanf("%d",&n); /*n是整型的*/
printf("please enter three grades of him:\n");
scanf("%f,%f,%f",&m,&c,&x); /*这三个存储的是float型的*/
printf("the student`s ave grade is:%f\n",(m+c+x)/3);
}
return 0;
}
{
char b[20];
float m,a,i,c,x;
int n;
for(i=0;i<=2;i++)
{
printf("Please enter a name of student:\n"); /*存储名字改用字符串*/
scanf("%s",&b);
printf("please enter a num of student:\n");
scanf("%d",&n); /*n是整型的*/
printf("please enter three grades of him:\n");
scanf("%f,%f,%f",&m,&c,&x); /*这三个存储的是float型的*/
printf("the student`s ave grade is:%f\n",(m+c+x)/3);
}
}