如何循环回到c程序的开头



如果用户选择否选项,我如何返回到程序的开始?请帮帮我。

#include <stdio.h>
#include <stdlib.h>
int main()
{
int choice;
printf("Are you done? 1 for Yes, 2 for No: ");
scanf("%d",&choice);

if (choice==1)
{

}
return 0;
}

使用while循环:

int main()
{
while (1)
{
int choice;
printf("Are you done? 1 for Yes, 2 for No: ");
scanf("%d", &choice);
if (choice == 1) {
break;
}
}
return 0;
}

不推荐使用goto,因为它会降低代码的可读性,但我仍然在这里写它,因为它也可以解决您的问题。

int main()
{
$START:
int choice;
printf("Are you done? 1 for Yes, 2 for No: ");
scanf("%d", &choice);
if (choice == 0) {
goto $START;
}
return 0;
}

了解有关goto:的更多信息

  • 使用goto有什么问题
  • GOTO仍然被认为是有害的

最新更新