我正在制作一个要求用户输入的程序。如果用户输入输入,则显示输入,然后完成程序。如何让程序从头开始? 我的代码是这样构建的:(只显示构建而不是代码本身)
please enter user input:
while (x != y)
{
if ( x == y )
{
printf("printing something");
}
else if (x > y )
{
printf("printing something");
}
else
{
printf("printing something");
}
}
答案 0 :(得分:2)
int main(void)
{
int num = 0;
while( 1 )
{
printf("This is a game to find the password. Start this game by trying to guess it with numbers from 1 - 100. The program will tell you if you are close or not.\n");
num = 0; // reset the num back to zero
while (num != 65)
{
printf("please enter a number:\n");
num = GetInt();
if (num == 65)
{
printf("Nice!!\n");
break; // exit the while (num != 65)
}
else if (num > 50 && num < 60 )
{
printf("almost there! go higher!\n");
}
}
}
}
答案 1 :(得分:0)
如果你不介意使用goto
:
HERE: please enter user input:
while (x!= y)
{
if ( x == y )
{
printf("printing something");
}
else if (x > y )
{
printf("printing something");
}
else
{
printf("printing something");
}
goto HERE;
}
在这个例子中,你可以看到你永远不会打破。你必须使用
小心goto
。
编辑:
上面的代码有错误。为了进入while
循环x!=y
。但是后来
检查始终为x==y
的{{1}}。所以:
false
编辑2:
您只有 ONE while循环
HERE: please enter user input:
while (true)
{
if ( x == y )
{
printf("printing something");
break;
}
else if (x > y )
{
printf("printing something");
}
else
{
printf("printing something");
}
goto HERE;
}
瓦尔特