我在 c 中創建了一個測驗程式,它向講師詢問問題以及答案,并將它們存盤到單獨的陣列中,例如“問題”和“答案”。螢屏被清除,問題被顯示給學生,學生的答案被保存在另一個陣列中,比如“std_answers”。然后,存盤在“answers”中的值與存盤在“std_answers”中的值進行測驗,每當有匹配時,學生在 3 上的得分就會增加。(要存盤的問題形式為“4*8/6”或“65 8/7”……或類似的形式)
#include<stdio.h>
#include<stdlib.h>
int main(){
int i,question[i][20],answers[i][20],std_answers[i][20],score;
/*demands and saves questions and answers into
the "question" and "Answers" arrays.*/
for(i=0; i<3; i ){
printf("Enter question %d: \n",i);
scanf("%s",&question[i]);
printf("Enter the answer for question %d: ",i);
scanf("%s",&answers[i]);
}
//hides all previous entries.
system("cls");
i=0;
//Asks questions stored in
// the"question" array.
for(i=0; i<3; i ){
printf("-------------------------");
printf("\n What is the answer to: \n");
printf(" %s \n", question[i]);
scanf("%d",&std_answers[i]);
//tests the answers.
if(answers[i]==std_answers[i]){
score=score 1;
printf("CORRECT Answer! \n\n");
printf("\n-------------------------");
}
else{
printf("WRONG Answer! \n");
printf("-------------------------");
}
}
printf("You scored %d on 3",score);
return 0;
}
在我應該用“answers”測驗存盤在“std_answers”中的值以找到匹配項的時候,這是語法錯誤,但是當我執行代碼時,即使匹配,它也會一直說“WRONG Answer”。
uj5u.com熱心網友回復:
這條線是非常錯誤的。
int i,question[i][20],answers[i][20],std_answers[i][20],score;
讓我們先重組它
int i;
int question[i][20];
int answers[i][20];
int std_answers[i][20];
int score;
3 個陣列(問題、答案、std_answers)設定為 'i' 通過 20。好吧,您沒有為 i' 指定值,它可能是 0,可能是 4765895。我假設 20 是允許問題20個字符長(似乎有點短)
其次,您想將問題存盤在“問題”陣列中,但它被宣告為 int。
第三個為什么是答案 i * 20,答案是數字而不是字串,所以你只需要其中的“i”。std_answers 相同
稍后您回圈詢問 3 個問題,那么您為什么要嘗試在這里制作可變大小的陣列。清理干凈
在#inlcudes 之后讓我們擁有
#define NUMBER_OF_QUESTIONS 3
然后
char question[NUMBER_OF_QUESTIONS][100];
int answers[NUMBER_OF_QUESTIONS;
int std_answers[NUMBER_OF_QUESTIONS];
int score = 0;
然后
for(int i=0; i<NUMBER_OF_QUESTIONS; i ){
printf("Enter question %d: \n",i);
scanf("
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/454876.html
上一篇:IF、CASE、WHEN陳述句根據SQL中的某個值回傳表的不同行
下一篇:If陳述句中的甜蜜警報
