#include<stdio.h>
#include<stdlib.h>
#include<time.h>voidGuess(){int a;int random=1+rand()%1000;while(1){scanf("%d",&a);if(a<random)printf("Too low.Try again.\n");elseif(a>random)printf("Too high.Try again.\n");else{printf("Excellent! You guess the number!\nWould you like to play again(y or n)?\n");scanf("%d",&a);}switch(getchar()){case'y':printf("Please type your first guess\n");Guess();case'n':printf("结束游戏\n");}}}intmain(){srand(time(0));printf("I have a number between 1 and 1000.\nCan you guess my number?\nPlease type your first guess.\n");Guess();return0;}
记录次数的猜数游戏
#include<stdio.h>
#include<stdlib.h>
#include<time.h>voidGuess(){int a,court=0;int random=1+rand()%1000;while(1){scanf("%d",&a);if(a<random){printf("Too low.Try again.\n");
court++;}elseif(a>random){printf("Too high.Try again.\n");
court++;}else{printf("Excellent! You guess the number!\nWould you like to play again(y or n)?\n");
court++;printf("\n");if(court<=10)printf("Either you know the secret or you got lucky!\n");elseprintf("You should be able to better!\n");scanf("%d",&a);}switch(getchar()){case'y':printf("Please type your first guess\n");Guess();case'n':printf("结束游戏\n");}}}intmain(){srand(time(0));printf("I have a number between 1 and 1000.\nCan you guess my number?\nPlease type your first guess.\n");Guess();return0;}