因此,我才刚刚开始学习C。我决定制作一个基本的数字猜谜游戏,该游戏会告诉您您是太高还是太低,并最多进行5次尝试。我已经工作了,但是我不知道该怎么做5次尝试。任何提示或帮助,将不胜感激。
#include <stdio.h> /* printf, NULL */
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
int main()
{
srand(time(NULL));
int r = ( rand() % 10 ) + 1; /* generates "random" number */
int number;
int c = 0;
printf( "Guess a number from one to ten!\n" );
do {
scanf( "%d", &number);
if (number == r) {
printf( "Correct! You win!!!\n" );
c = 1; /* ends loop if correct */
}
else if (number > r) {
printf( "Too high! Try again!\n" );
}
else {
printf( "Too low! Try again!\n" );
}
}
while ( c == 0 );
return 0;
}
在循环条件中添加一个计数器:
int counter = 0;
do {
....
counter++;
}
while ( c == 0 && counter < 5 );
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句