求大神帮忙用for语句编一道题
猜数:如果猜了5次仍未猜中的话,则停止本次猜数,然后继续猜下一个数。每次运行程序可以反复猜多个数,直到操作者想停止时才结束。如果猜了5次仍未猜中的话,则停止本次猜数,然后继续猜下一个数。每次运行程序可以反复猜多个数,直到操作者想停止时才结束。
2015-06-10 22:36
2015-06-10 23:22
原来是要for,修改下下
2015-06-10 23:33
2015-06-10 23:37
程序代码:
#include<stdio.h>
#include<stdlib.h>
int main(void){
int randint, guess;
char ch = 0;
srand((unsigned)time(NULL));//设置随机数种子
while( ch != 'q'){
printf("************游戏开始**********\n");
randint = rand()%10;
int i = 0;
for(; i<5; ++i){
printf("输入你要猜的数字:");
if(scanf("%d", &guess) == 1 && guess == randint){
while(getchar() != '\n');
printf("猜对了\n\n");
break;
}
while(getchar() != '\n');
printf("猜错了,再猜一次\n");
}
if(i == 5)
printf("连续猜错5次,游戏失败\n\n");
printf("按q退出游戏,其他键继续\n");
ch=getchar();
if(ch != '\n')
while(getchar()!='\n');
}
printf("Bye\n");
return 0;
}

2015-06-11 12:06
2015-06-11 15:04