简体   繁体   中英

Loop with "continue" or back to condition

A have an error there. I tried to write a continue in loop and this didn't working(or jump to back). I'm begginer soo please be do not be angry after read this. Please help me!

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

It's a really bad practice to jump in the code like this. Have you consider using a recursive method?

If you want to make use of continue properly , you should read this question .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM