2013-06-26 45 views
-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。

+1

请格式化您的代码。 – Elazar

+1

并告诉我们你的问题是什么。 – 2013-06-26 07:23:46

+0

(哦,我知道了。通常的'scanf(“%c”)' - extranewline - whynotterminating“问题...) – 2013-06-26 07:24:29

回答

1

设置total=0在进入do-while循环之前,得到正确的total

+0

也扩展for循环包含'if(r == 7)'语句(我的解法假设你想计算在所有生成的随机数中出现的7的总数) – amulous

+0

谢谢,for循环的扩展工作。 –