要打印例如 等級:4/5 80% 程序問用戶他們想要多少數學問題來解決,並打印出「冤屈數/數權利「和他們的等級。我覺得我沒有我的數學就在代碼,最終導致其打印出來,例如: 等級:4/5 -7446528%的C程序:簡單的數學程序
+++++++++++++++ ++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++
# include <stdio.h>
int main()
{
int NumberOfTimes, AddAns, SubAns, AddCorrect=0, SubCorrect=0, CorrectAnsAdd, CorrectAnsSub, TotalCorrect, TotalWrong, Add;
int i,a,b,c,d,e,f,g;
float percent;
printf("\n");
printf("-------------------MATH QUIZ------------------------\n");
printf("Enter the number of Math problems you want to solve:"); //enters the # of problems the program produces
scanf("%d", &NumberOfTimes);
printf("\n");
srand(time(NULL));
//Random number generator
for (i=0;i<NumberOfTimes;++i)
{
b = rand() %3 + 1;
c = rand() %3 + 1;
a = rand() %2 + 1;
//Random addition problems
if (a == 1)
{
printf("%d + %d = ", b,c);
scanf("%d", &AddAns);
d = b + c;
if (AddAns == d)
{
printf(" +Correct\n");
AddCorrect = AddCorrect + 1;
}
//Random subtraction problems
else
{
printf(" +Wrong, it was %d\n", d);
AddIncorrect = AddIncorrect + 1;
}
}
if (a == 2)
{
printf("%d - %d = ", b,c);
scanf("%d", &SubAns);
g = b - c;
//Produces right or wrong answers
if (SubAns == g)
{
printf(" +Correct\n");
SubCorrect = SubCorrect + 1;
}
else
{
printf(" +Wrong, it was %d\n", g);
SubIncorrect = SubIncorrect + 1;
}
}
}
//Producing the output to wrong/right numbers and grade percentage
TotalCorrect = AddCorrect + SubCorrect;
printf("\n");
printf("Grade: %d/%d\n",TotalCorrect,NumberOfTimes);
printf("\n");
percent=NumberOfTimes/TotalCorrect;
printf("%d percent \n", percent);
return 0;
}
使用'%f'打印浮點值。即'printf(「%f%\ n」,百分比);'。 – Marian
你對百分比的計算是錯誤的......應該是數字正確除以問題的數量,而不是相反......並且它需要乘以100,並且其中的某些內容需要「浮動」以便它贏得不能用整數除法計算。你也需要'%f'格式說明符來打印它,而不是'%d',它是整數。 – Dmitri
目前還不清楚你在問什麼。問題通常以'?'結尾。閱讀[問]。 – xenteros