當我有關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
爲什麼不使用數組和循環? –
另外,您需要了解賦值('=')和比較等號('==')之間的區別。 –