如何在循环中创建一段代码只在Java 8中执行一次?

时间:2017-05-11 15:45:08

标签: java

我的代码目前看起来像这样

public void method() {

while (a condition) {

    if()  {
              //something

}       else {

        //something

 }
}      

  //I have a code here which I want to execute only once
}

3 个答案:

答案 0 :(得分:1)

方法1 - 使用const CORRIDOR = 0; const WALL = 1; const EXIT = 2; // board - 2D array // start - [row, column] location of start point function findPath(board, start) { let seenPath = new Set(); findPathRecur(board, start, seenPath) return Array.from(seenPath); } function findPathRecur(board, point, seenPath) { let row = point[0], column = point[1]; // Base Case if (!isValidPathPoint(board, point, seenPath)) { return false; } if (board[row][column] === EXIT) { seenPath.add(point.toString()); return true; } // console.log("Curr -", point, ", Seen -", Array.from(seenPath)); seenPath.add(point.toString()); let leftColumn = [row, column - 1], rightColumn = [row, column + 1], topRow = [row - 1, column], bottomRow = [row + 1, column]; if (findPathRecur(board, leftColumn, seenPath)) { return true; } if (findPathRecur(board, rightColumn, seenPath)) { return true; } if (findPathRecur(board, topRow, seenPath)) { return true; } if (findPathRecur(board, bottomRow, seenPath)) { return true; } seenPath.delete(point.toString()); return false; } // Check if the point is on valid path function isValidPathPoint(board, point, seenPath) { let row = point[0]; let column = point[1]; // Check if point exists if (board[row] != null && board[row][column] != null) { // To avoid cycle if (!seenPath.has(point.toString())) { // Not a Wall if (board[row][column] !== WALL) { return true; } } } return false; } // Test Program let maze = [ [1, 1, 1], [0, 0, 1], [1, 2, 1] ]; let testStart = [ [1,0], [1,1], [2,1], [0,0], [5,5] ]; testStart.forEach(function(start, i) { console.log("Test Case:",i); console.log("\tStart -", start, "Path -", findPath(maze, start)); })标志:

boolean

方法2 - 将private boolean first = true; public void method() { while (a condition) { if() { //something } else { //something } } if (first) { //I have a code here which I want to execute only once first = false; } } 参数传递给此方法:

boolean

答案 1 :(得分:0)

简单的方法是使用布尔标志!

if (flag)
{
   //I have a code here which I want to execute only once
   flag = false;
}

答案 2 :(得分:0)

我们可以通过使用一个简单的布尔标志来实现这一点,如下面的一个简单示例所示。我已对每一行进行了评论,以便您了解为何以这种方式完成。希望您发现此代码段有用。

public void method(){
    boolean alreadyRun = false; //<--Declare a boolean outside the while loop as false
    int i = 1; //a variable which will drive the odd-even printer
    while(i <= 5){ //Nothing special here, just a normal while loop
        if(i % 2 == 0){ //loop logic in this case checking even-odd
            System.out.println("I am even");
        }else{
            System.out.println("I am odd");
        }
        i++;//loop counter increment which is also normal
        if(!alreadyRun){ //this is where the magic happens, you check if the value of alreadyRun is false or not
                         //if false then execute the print statement below and then also set the alreadyRun
                         //variable to true so on the next loop iteration, the value will be true and
                        //the if body will be skipped which is the thing you want
            System.out.println("You get to see me only once"); //A statement that prints only once
            alreadyRun = true; //setting the boolean to true to bar the execution of this if body over the next iterations of the while loop
        }
    }
}
相关问题