当我有关scanf()
虽然使用多个scanf()的它跳过scanf()的其余
它只是跳过第二,第三和之后的任何其他scanf()
输入一个值。
这是我的代码:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
float manyTest, gr1, gr2, gr3, gr4, gr5, gr6, gr7, gr8;
manyTest = gr1 = gr2 = gr3 = gr4 = gr5 = gr6 = gr7 = gr8 = 0;
printf("How many tests you wanna average? (Minimum 1 Maximum 8)\n");
scanf(" %f", &manyTest);
if (manyTest <= 0) {
printf("The Minimum is 1!\n");
} else
if (manyTest > 8) {
printf("The Maximum is 8!\n");
} else {
if (manyTest = 1) {
printf("Write down your grades on those tests:\n");
scanf(" %f", &gr1);
} else
if (manyTest = 2) {
printf("Write down your grades on those tests:\n");
scanf(" %f", &gr1);
scanf(" %f", &gr2);
} else
if (manyTest = 3) {
printf("Write down your grades on those tests:\n");
scanf(" %f", &gr1);
scanf(" %f", &gr2);
scanf(" %f", &gr3);
} else
if (manyTest = 4) {
printf("Write down your grades on those tests:\n");
scanf(" %f", &gr1);
scanf(" %f", &gr2);
scanf(" %f", &gr2);
scanf(" %f", &gr3);
scanf(" %f", &gr4);
} else
if (manyTest = 5) {
printf("Write down your grades on those tests:\n");
scanf(" %f", &gr1);
scanf(" %f", &gr2);
scanf(" %f", &gr2);
scanf(" %f", &gr3);
scanf(" %f", &gr4);
scanf(" %f", &gr5);
} else
if (manyTest = 6) {
printf("Write down your grades on those tests:\n");
scanf(" %f", &gr1);
scanf(" %f", &gr2);
scanf(" %f", &gr2);
scanf(" %f", &gr3);
scanf(" %f", &gr4);
scanf(" %f", &gr5);
scanf(" %f", &gr6);
} else
if (manyTest = 7) {
printf("Write down your grades on those tests:\n");
scanf(" %f", &gr1);
scanf(" %f", &gr2);
scanf(" %f", &gr2);
scanf(" %f", &gr3);
scanf(" %f", &gr4);
scanf(" %f", &gr5);
scanf(" %f", &gr6);
scanf(" %f", &gr7);
} else
if (manyTest = 8) {
printf("Write down your grades on those tests:\n");
scanf(" %f", &gr1);
scanf(" %f", &gr2);
scanf(" %f", &gr2);
scanf(" %f", &gr4);
scanf(" %f", &gr5);
scanf(" %f", &gr6);
scanf(" %f", &gr7);
scanf(" %f", &gr8);
}
float avg = (gr1 + gr2 + gr3 + gr4 + gr5 + gr6 + gr7 + gr8)/manyTest;
printf("Your average grade is: %.2f\n", avg);
}
system("pause");
return 0;
}
什么是错我的代码,它跳过scanf()
的休息吗? 我想要的代码是询问他们想要测试多少次,然后取这个数字(在1到8之间),然后进行测试,然后平均分数。
让说:
How many test...?
2
Write down your grades:
100
90
Your average grade is: 95.00
,但它确实是:
How many test.....?
2
Write down your grades:
90
Your average is: 90.00
,甚至不让它到另一个变量的信息。
为什么在它们每个中的'%f'之前的空格?删除它们。 – ameyCU
为什么不使用数组和循环? –
另外,您需要了解赋值('=')和比较等号('==')之间的区别。 –