给你一个完整的程序:
#include
#include
#include
int main()
{ int num;
char c;
printf("press 'y' to begin : ");
srand(time(NULL));
while((c=getchar())!='n')
{ num=rand()%10;
if(num>0)
printf("success: num=%d",num);
else
printf("error: the num=0,please continue:\n");
printf("\ncontinue? press 'y' or 'n'\n " );
c=getchar(); //按“ENTER 键的时候 读如了换行符,这句可跳过换行符。
}
return 0;
}
#include
srand((unsigned)time(NULL)); ——为了避免重复
随机数产生用rand()%10;
include
srand((unsigned)time(NULL)); ——为了避免重复
(rand()/RND_MAX)*10