猜数字游戏
#include<stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
int main(void){
int a,b,c;
c = 0;
srand(time(NULL));
a = rand()%100+1;
printf("%d",a);
while (true){
printf("输入一个数字:\n");
scanf("%d",&b);
c++;
if(b<a) {
printf("猜小了!\n");
continue;
}
else if(b>a){
printf("猜大了!\n");
continue;
}
else {
printf("正确!!,猜了%d次",c);
break;
}
}
}
#include<stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
int main(void) {
int a, b, c = 0, d;
char e;
srand(time(NULL));
do {
c = 0;
a = rand() % 100 + 1;
printf("%d", a);
do {
printf("输入一个数字:\n");
d = scanf("%d", &b);
while (d != 1) {
while (getchar() != '\n');
printf("请输入一个数字!!!!");
d = scanf("%d", &b);
}
c++;
if (b < a) {
printf("猜小了!\n");
} else if (b > a) {
printf("猜大了!\n");
} else {
printf("正确!!,猜了%d次", c);
}
} while (b != a && c < 10);
printf("y or Y?");
scanf("%c",&e);
} while (e == 'Y'||e =='y');
return 0;
}
|