1.无限猜数版
1.1
#include#include #include int main() { int magic; int guess; unsigned int seed; int counter; srand(time(NULL)); counter=0; magic=rand()%100+1; //printf("%dn",magic);答案 do{ printf("Please guess a number:"); scanf("%d",&guess); counter++; if(magic>guess) { printf("Wrong.Too small.n"); } if(magic 1.2
#include#include #include int main() { int magic; int guess; unsigned int seed; int counter; srand(time(NULL)); counter=0; magic=rand()%100+1; //printf("%dn",magic);答案 do{ printf("Please guess a number:"); scanf("%d",&guess); counter++; if(magic>guess) { printf("Wrong.Too small.n"); } if(magic 2.限制猜数次数版
10次
#include#include #include int main() { int magic; int guess; unsigned int seed; int counter; srand(time(NULL)); counter=0; magic=rand()%100+1; printf("%dn",magic); do{ printf("Please guess a number:"); scanf("%d",&guess); counter++; if(magic>guess) { printf("Wrong.Too small.n"); } if(magic 3.错了重来版
#include#include #include int main() { int magic; int guess; unsigned int seed; int counter; char ch; srand(time(NULL)); do{ counter=0; magic=rand()%100+1; //printf("%dn",magic);答案 do{ printf("Please guess a number:"); scanf("%d",&guess); counter++; if(magic>guess) { printf("Wrong.Too small.n"); } if(magic ch的%c前要有一个空格。把前面的回车消除掉。
4.好好输入版
以上如果输入的不是数字会产生错误。
输入q。
防止输入非法字符。
printf("Please guess a number:"); a=scanf("%d",&guess); while(a!=1) { while(getchar()!='n');3 printf("Please guess a number properly:"); a=scanf("%d",&guess); }完整↓
#include#include #include int main() { int magic; int guess; unsigned int seed; int counter; char ch; int a; srand(time(NULL)); do{ counter=0; magic=rand()%100+1; //printf("%dn",magic);答案 do{ printf("Please guess a number:"); a=scanf("%d",&guess); while(a!=1) { while(getchar()!='n');3 printf("Please guess a number properly:"); a=scanf("%d",&guess); } counter++; if(magic>guess) { printf("Wrong.Too small.n"); } if(magic
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)