如果 switch 语句中子句被禁用怎么办?
我很抱歉这个标题,我不知道该怎么称呼它。
我的问题是这样的,我有一个 switch 语句,它随机选择一个问题类型,如下所示:
questionType[count] = rad.nextInt(4);
switch (questionType[count])
{
case 0:
questionArray[count] = new Times(count + 1);
((Times) questionArray[count]).askQuestion(question);
break;
case 1:
questionArray[count] = new Add(count + 1);
((Add) questionArray[count]).askQuestion(question);
break;
case 2:
questionArray[count] = new Minus(count + 1);
((Minus) questionArray[count]).askQuestion(question);
break;
case 3:
questionArray[count] = new Divide(count + 1);
((Divide) questionArray[count]).askQuestion(question);
break;
}
它的作用是随机选择一个问题,然后将其添加到下一个数组中,这是伪代码版本:
questionType = random(4)
case questionType == 0
do something
break
case questionType == 1
do something
break
case questionType == 2
do something
break
case questionType == 3
do something
break;
现在让我们说我不想要任何问题1或2,我该如何使其生效?我在想类似的事情:
START:
questionType = random(4)
case questionType == 0
if(0 enabled)
{
do something
break
}
case questionType == 1
if(1 enabled)
{
do something
break
}
case questionType == 2
if(2 enabled)
{
do something
break
}
case questionType == 3
if(3 enabled)
{
do something
break;
}
else goto start
但是这是非常低效的代码并且使用了 goto,有什么替代方案?
I'm sorry for the title, I wasn't sure what to call it.
My problem is this, I have a switch statement which randomly chooses a question type as so:
questionType[count] = rad.nextInt(4);
switch (questionType[count])
{
case 0:
questionArray[count] = new Times(count + 1);
((Times) questionArray[count]).askQuestion(question);
break;
case 1:
questionArray[count] = new Add(count + 1);
((Add) questionArray[count]).askQuestion(question);
break;
case 2:
questionArray[count] = new Minus(count + 1);
((Minus) questionArray[count]).askQuestion(question);
break;
case 3:
questionArray[count] = new Divide(count + 1);
((Divide) questionArray[count]).askQuestion(question);
break;
}
What this does is randomly choose a question and then add it to the next array, here is the pesudocode version:
questionType = random(4)
case questionType == 0
do something
break
case questionType == 1
do something
break
case questionType == 2
do something
break
case questionType == 3
do something
break;
Now lets say I do not want any 1's or 2's, how do I put that into effect? I was thinking something along the lines of this:
START:
questionType = random(4)
case questionType == 0
if(0 enabled)
{
do something
break
}
case questionType == 1
if(1 enabled)
{
do something
break
}
case questionType == 2
if(2 enabled)
{
do something
break
}
case questionType == 3
if(3 enabled)
{
do something
break;
}
else goto start
But this is very inefficient code and uses a goto, what are the alternatives?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
几种实现方法但是
如果您有大量问题,以及这些问题是否已启用。
然后从中提取启用并使用它的计数作为 rad 的参数。
所以你启用的问题将是 3 & 4 但会占据位置 0 和 4已启用问题列表中的 1 个。
A few ways of implementing but
if you had a store of questions, and whether they were enabled.
Then extract enabled from that and use it's count as the argument for rad.
so you enabled Questions would be 3 & 4 but would occupy positions 0 & 1 in the list of enabled questions.
“启用”全局或静态布尔数组。循环随机数生成,直到获得一个索引,该索引可寻址“Enabled”数组中的真实值。由于特殊情况 - '0' - 返回错误或引发异常,请保留已启用选项的总数,以防止徒劳地尝试找到有效选项时出现无限循环。
'Enabled' global or static array of boolean. Loop around the random number generation until you get an index that addresses a true value in the 'Enabled' array. Keep a count of the total number of enabled choices because of the special case - '0' - return an error or raise exception to prevent endless looping in a futile attempt to find a valid choice.