ユーザーの要求に応じて、整数 0 から 9 の新しいランダム順列を表示するプログラムを作成します。たとえば、プログラムの出力は次のようになります。
ユーザーが「no」と入力すると、プログラムは 7 がいくつ出力されたかを出力する必要があります。
私のコード:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main()
{
int i , total , r;
char ans;
srand(time(NULL));
do{
for( i=0 ; i < 10 ; i++)
{
r= (rand()%(9-1+1)) + 1;
printf ("%d ",r);
}
total =0;
if (r==7) // Here how can I correct this so total will increase every time
{ // there is a 7 in the string
total++;
}
printf("\nAnother permutation: y/n?\n");
scanf(" %c",&ans);
if (ans != 'y')
{
printf("Bye!\n");
printf("The number of 7's is: %d", total);
}
}while(ans=='y');
return 1;
}
コードに問題があります。このプログラムで != 'y' の後に表示されている 7 をインクリメントするにはどうすればよいですか。