gpt4 book ai didi

c# - Break inside switch 无法终止 FOR 循环

转载 作者:太空狗 更新时间:2023-10-29 22:04:52 26 4
gpt4 key购买 nike

<分区>

我有一个代码片段:

int n = 0;
for (int i = 0; i < 50;i++)
{
n = checkStatus();
switch (n)
{
case 1:
break;
break;//This is unreachable and so i cannot Terminate the For Loop within the SWITCH
}
}

如评论中所述,我不能直接从 Switch 终止 For 循环,只有当我声明一个 bool 值并在 Switch 测试结束时才可以

if(LoopShouldTerminate)
break;

PS:也许我很困惑!

[发布]我收到消息,问题已解决,但我想假设在 for 循环中使用 Switch 不是一个好主意,因为我从很多开发人员那里听说我应该在我得到期望的结果,所以使用 switch 需要额外的 bool 值或将 Int i 值直接推到 50,但是如果我们使用 while 循环会发生什么?

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com