-1

ユーザーの要求に応じて、整数 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 をインクリメントするにはどうすればよいですか。

4

1 に答える 1

1

total=0do-while ループに入る前に設定して、正しいtotal.

于 2013-06-26T07:46:23.107 に答える