使用 "continue" 循环或返回条件
Loop with "continue" or back to condition
那里有一个错误。我试着写一个 continue in loop 但这没有用(或跳回)。我是初学者所以请不要在阅读本文后生气。请帮助我!
somepoint: { Random first = new Random();
int b = first.nextInt(8);
if (b==0)
{
x=x+20;
for (int c=0; c<100; c++)
{
if (x>=950)
{
JOptionPane.showMessageDialog(null, "Winner is white car!");
return;
}
else
{
continue somepoint; //cannot be used outside, help
}
}
}
else if (b==1)
{
x2=x2+20;
for (int c=0; c<100; c++)
{
if (x2>=950)
{
JOptionPane.showMessageDialog(null, "Winner is red car!");
return;
}
else
{
continue somepoint; //cannot be used outside,
}
}
}
else if (b==2) ...
像这样跳入代码是一种非常糟糕的做法。您是否考虑过使用递归方法?
如果您想正确使用 continue
,您应该阅读此 question。
那里有一个错误。我试着写一个 continue in loop 但这没有用(或跳回)。我是初学者所以请不要在阅读本文后生气。请帮助我!
somepoint: { Random first = new Random();
int b = first.nextInt(8);
if (b==0)
{
x=x+20;
for (int c=0; c<100; c++)
{
if (x>=950)
{
JOptionPane.showMessageDialog(null, "Winner is white car!");
return;
}
else
{
continue somepoint; //cannot be used outside, help
}
}
}
else if (b==1)
{
x2=x2+20;
for (int c=0; c<100; c++)
{
if (x2>=950)
{
JOptionPane.showMessageDialog(null, "Winner is red car!");
return;
}
else
{
continue somepoint; //cannot be used outside,
}
}
}
else if (b==2) ...
像这样跳入代码是一种非常糟糕的做法。您是否考虑过使用递归方法?
如果您想正确使用 continue
,您应该阅读此 question。