问题描述
- c语言简单编程 求解答
-
guess the number (1 points)
? Define a number between one and one hundred
? If a user input a number, the computer output “Too high!” or
“Too low!”
? If you guess the number in 6 tries, print “That’s Correct!” ? If not, print “Sorry, bye bye~”
解决方案
解决方案二:
#define NUM 12
printf("I'm thinking of a number between and including 1 to 100
");
printf("Can you guess the number in 6 tries?
");
for( int i= 0;i < 6;++i)
{
printf("Enter guess number %d:",i+1);
int inputNum;
scanf("%d",&inputNum);
if( inputNum > NUM )
printf("Too high!
");
else if( inputNum < NUM )
printf("Too low!
");
else
{
printf("That's Corrent!
");
break;
}
}
解决方案三:
#include <stdio.h>
#include <time.h>
//生成一个1 - 100 的随机值
int GetRandNum(void)
{
srand((unsigned int)time(0));
return (rand()%100) + 1;
}
//猜测系统生成的随机数
void GuessSysRandNum()
{
int a = GetRandNum();
int i = 1;
int b;
while(i < 7)
{
printf("Enter guess number %d:", i);
scanf("%d", &b);
if(a > b)
{
printf("Too low!
");
}
else if(a < b)
{
printf("Too high!
");
}
else
{
printf("That's Correct!
");
return;
}
i++;
}
printf("Sorry, bye bye ~
");
}
int main(void)
{
GuessSysRandNum();
return 0;
}
时间: 2024-11-05 12:14:59