随机生成一个数字,猜大猜小
#include <stdio.h>
#include <time.h>
int main(){
int num;
srand(time(NULL));
num=rand()%99+1;
//printf("%d\n",num);
int min,max,user=0;
min=0;
max=100;
printf("请猜一个(0,100)区间内的整数(按0结束):");
for(;;){
scanf("%d",&user);
if(user==0){
printf("game over\n");
break;
}else if(user<=min || user>=max){
printf("你猜的数字不在范围内,请继续猜(%d,%d)内的数字\n",min,max);
}else if(user>num){
max=user;
printf("你猜的数字太大了,请继续猜(%d,%d)内的数字\n",min,max);
}else if(user<num){
min=user;
printf("你猜的数字太小了,请继续猜(%d,%d)内的数字\n",min,max);
}else {
printf("恭喜你猜对了!\n");
break;
}
}
return 0;
}
石头剪刀布
#include <stdio.h>
#include <time.h>
int main (){
int user;
int com;
printf(" 输入对应数字进行剪刀(2)石头(0),布(1)!");
scanf("%d",&user);
srand(time(NULL));
com = rand()%3;
switch(com){
case 0: printf("电脑出了石头\n");break;
case 1: printf("电脑出了布\n");break;
case 2: printf("电脑出了剪刀\n");break;
}
switch (user){
case 0:{
switch (com){
case 0: printf("again\n");break;
case 1: printf("lose\n");break;
case 2: printf("win\n");break;
}
} break;
case 1:{
switch (com){
case 0: printf("win\n");break;
case 1: printf("again\n");break;
case 2: printf("lose\n");break;
}
}break;
case 2:{
switch(com){
case 0: printf("lose\n");break;
case 1: printf("win\n");break;
case 2: printf("again\n");break;
}
}break;
default: printf("no\n");
}
return 0;
}