当我为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
并且甚至不让它获取其他变量的信息。
答案 0 :(得分:2)
实际上,在您的代码中,罪魁祸首不是scanf
,而是wrong
个等式
else if (manyTest = 8) //WROOOOONNGGGG!! =( =( =( =(
改为双等号......
else if (manyTest == 8) //correct! =)
因为你真正想要的是比较
答案 1 :(得分:1)
问题是以下语句将始终与第一个if。匹配。
首先,您需要使用==进行比较,因为使用=只会使这成为一项任务。
其次,赋值manyTest = 1
将始终评估为true,因为它是如何工作的。因此,当您到达if (manyTest = 1)
时,条件将为真,因此您将输入第一个只有一个scanf的情况。你永远不会到达其他if语句并测试这些条件。
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);
}
...
将来调试时你可以在每个if语句中使用printf中的不同文本来更好地理解出错的地方。
答案 2 :(得分:0)
您的代码中存在一个经典错误:
您在测试中使用赋值运算符=
而不是比较运算符==
。 C非常灵活:它允许在测试表达式中进行分配,这会导致您遇到的经典错误。你被咬了一次,所以现在你知道你应该键入什么。任何人都可以打错,有一种非常有效的方法来防止这个问题:编译并启用警告(例如:gcc -Wall -Wextra -Werror
)。