使用C,我如何隨機顯示數組中的值,因爲這些值中的每一個都有唯一對應的值,而不重複顯示任何這些值? 使用C,我的目標是隨機顯示一個問題,一次一個,設置一定量的迭代。我創建了一個包含問題及其四個可能答案的數組。我還創建了一個數組,爲每個問題提供正確的答案。考慮到每個值都具有唯一的對應值,我該如何在數組中隨機顯示值?
非常感謝你們......你們已經非常有幫助
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
int random();
int l, qs[10];
int main()
{
for (l=0;l<10;++l)
qs[l]=0;
srand(time(NULL));
char questions [] [50] ={"aa \n\na)\n\nb)\n\nc)\n\nd)",\"bb \n\na)\n\nb)\n\nc)\n\nd)", "cc \n\na)\n\nb)\n\nc)\n\nd)", \
"dd \n\na)\n\nb)\n\nc)\n\nd)", "ee \n\na)\n\nb)\n\nc)\n\nd)", \
"ff \n\na)\n\nb)\n\nc)\n\nd)", "gg \n\na)\n\nb)\n\nc)\n\nd)", \
"hh \n\na)\n\nb)\n\nc)\n\nd)", "ii \n\na)\n\nb)\n\nc)\n\nd)", \
"jj \n\na)\n\nb)\n\nc)\n\nd)"};
char answers [10] = {'a','b', 'b','d','c','b','d','b' ,'c', 'b'};
int i, j;
char ans;
int score = 0;
printf ("Read each question carefully and choose your best answer.");
for (i=1;i<6;i++)
{
j = random();
fflush(stdin);
clrscr();
printf ("\n %d %s \n\n", i, questions [j]);
printf ("\n Enter Answer: ");
do
{
ans = tolower(getchar());
}while ((ans < 'a') || (ans > 'd'));
printf ("\nYou chose: %c ", ans);
if (ans == answers[j])
{score = score + 1;
printf ("\n\nCorrect. %d Mark/s", score);
}else{printf ("\n\nIncorrect. 0 Mark/s");}
printf("\n\nPress Enter for next question...");
getch();
}
getch();
return 0;
}
random()
{
int k;
do
{k=rand()%10;
}while (qs[k]!=0);
qs[k]= 1;
return k;
}
好的無限'while'循環如果其中一個條件沒有通過。 – AusCBloke