当我为 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
甚至不让它获取其他变量的信息。
最佳答案
实际上,在您的代码中,罪魁祸首不是 scanf
,而是这些错误的
方程
else if (manyTest = 8) //WROOOOONNGGGG!! =( =( =( =(
改为使用双等号...
else if (manyTest == 8) //correct! =)
因为你真正想要的是比较
关于c - 在使用多个 scanf() 时,它会跳过其余的 scanf(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34411284/