Is it bad style to use 'return' to break a for loop in Java?

后端 未结 11 1602
栀梦
栀梦 2020-12-18 21:01

I\'m doing a project with basic Java for a CS class. The project has a for loop nested inside a while loop.

I am not allowed to use break as a wa

11条回答
  •  情话喂你
    2020-12-18 21:44

    I'd imagine that your tutor wanted that you use the break condition in the condition part of the for loop. You need to declare this condition before the loop and change it inside the loop.

    boolean stop = false;
    
    for (...; ... && !stop; ...) {
        ...
        if (...) {
            stop = true;
        }
    }
    

提交回复
热议问题