猜数字

it2025-02-11  27

/* ============================================================================ Name : test.c Author : Version : Copyright : Your copyright notice Description : Hello World in C, Ansi-style ============================================================================ */ #include <stdio.h> #include <stdlib.h> #include<time.h> void main() { int temp;//存放用户输入的猜测的数值 int tar;//存放产生的随机数作为目标数 int count = 0;//记录用户猜测的次数 srand((unsigned int) time (NULL)); tar = rand()%10000 + 1;//随机数范围1-10000 //while循环条件为1代表直接开始循环 while(1){ count++; printf("input a number"); scanf("%d",&temp); if(temp > tar) { printf("cai da le\n"); } else if(temp < tar) { printf("cai xiao le\n"); } else { printf("bingo,cai le %d ci",count); break;//猜对了才执行break跳出循环 } } }
最新回复(0)