goto 跳过

来源:百度知道 编辑:UC知道 时间:2024/06/03 13:42:52
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
main()
{ char c;
clock_t start ,end;
time_t a,b;
duoble var;
int i,guess;
srand (time (NULL));
printf ("do you want to play it.('y'or'n')\n");
loop:
while((c=getchar())=='y')
{ i=rand()%100;
printf("\n please input number you guess:\n");
start=clock();
a=time (NULL);
scanf("%d",&guess);
while (guess!=i)
{ if (guess>i)
{printf ("please input a little smaller.\n");
scanf ("%d",&guess);}
else
{printf ("please input a little bigger .\n");
scanf ("%d",&guess);}
}
end=clock();
b=time(NULL);
printf ("\i:It tor

用getch()替换getchar()函数,在猜对以后,连续按两次'y'可以继续游戏。

getch()不需要按回车
getchar()需要按回车返回,就是因为这个回车,导致while((c=getchar())=='y') 条件不成立,没能进入循环。
这个回车保存在输入缓冲区,下一次调用getchar函数就会返回回车符,可以如下添加调试语句查看:
//...
while((c=getchar())=='y')
{
}
printf("you enter %d\n", c);/*回车符ascii码是10 */
printf("\n do you want to try it again ? (\"y\"or \"n\")\n");
if ((c=getchar())=='y')
goto loop;
}

不能用 loop 换个别的标记

这是C++吗,只是学了C,没有学这个,有点难看懂