【问题标题】:Loop with "continue" or back to condition循环“继续”或返回条件
【发布时间】:2015-11-06 15:20:27
【问题描述】:

那里有一个错误。我试图在循环中写一个继续,但这没有用(或跳回)。我是初学者所以请在读完这篇文章后不要生气。请帮帮我!

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) ...

【问题讨论】:

  • 人们通常认为像这样跳入代码是不好的做法。您是否尝试过使用其他循环结构(例如 while 循环)来包含代码?
  • @phflack noo,好的,我试试这个

标签: java if-statement for-loop continue


【解决方案1】:

像这样跳入代码是一种非常糟糕的做法。您是否考虑过使用递归方法?

如果你想正确使用continue,你应该阅读这个question

【讨论】:

    猜你喜欢
    • 2017-11-21
    • 1970-01-01
    • 2017-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-16
    • 2020-11-29
    • 2023-01-11
    相关资源
    最近更新 更多