这是一个 C 程序,用于计算离散频率分布的算术平均值。 该程序按预期运行,但无法理解 main() 函数中“While”在该程序中的作用。
void main()
{
int choice;
while(1)
{
printf("\n\tThe Mean for Discrete Distribution ");
printf("\n\n1.Direct Method \n2.Shortcut Method \n0.Exit ");
printf("\nEnter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
getdata();
direct();
break;
case 2:
getdata();
shortcut();
break;
case 0:
exit(0);
break;
default:
printf("\nEnter valid choice!");
break;
}
}
}
程序运行得很好,但我只是不明白“while”在这里做什么。程序很大,所以我没有写函数。
最佳答案
由于 while(1)
总是评估为 true
,该程序不断提示用户“输入您的选择”,直到用户点击 0
。当输入 0
时,整个程序停止,因为将调用函数 exit(0)
。
关于c - 'while' 在这个程序中做什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52681759/