编写一个程序,该程序根据用户的请求显示整数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。
请格式化您的代码。 – Elazar
并告诉我们你的问题是什么。 – 2013-06-26 07:23:46
(哦,我知道了。通常的'scanf(“%c”)' - extranewline - whynotterminating“问题...) – 2013-06-26 07:24:29