这是一个求离散频率分布算术平均值的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 - 在此程序中“正在”做什么?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52681759/