简体   繁体   中英

How to get program to stop running once its met the goal - Java

I am trying to get the program to stop once it has met this condition fully, i run the program around 100,000 times because this is one of a hundred thousands of cases. But I'm not sure exactly how to make the program stop if it has met. I know if its is met it prints out a way i identify this case, but since i am running over 100,000 times the console does not remember all off the cases it has been through.

`public static void stop_running(int cube[][]){
    try {
        if(cube[5][0] == 5 && cube[5][2] == 5 && cube[3][8] == 5 && cube[5][1] == 5 && cube[5][3] == 5 && cube[5][5] == 5);

        }
    catch (Exception stop){

        System.out.println("CHECK");
    }`

Simply return.

public static void stop_running(int cube[][]){
try {
    if(cube[5][0] == 5 && cube[5][2] == 5 && cube[3][8] == 5 && cube[5][1] == 5 && cube[5][3] == 5 && cube[5][5] == 5) {
        return; 
     }
 }
catch (Exception stop){

    System.out.println("CHECK");
}`

My answer is similar to Richard's, but I'm going to assume that you haven't shown us the whole code. Return a value from stop_running() that tells your calling method whether to stop or not.

public static void more_code(int cube[][]) {
    while( true ) {
       // big loop
       if( stop_running( cube )) return;
    }
}

public static boolean stop_running(int cube[][]) {
    if(cube[5][0] == 5 && cube[5][2] == 5 && cube[3][8] == 5 && cube[5][1] == 5 && cube[5][3] == 5 && cube[5][5] == 5)
        return true;
    else
        return false;
}

Returning (as proposed by Richard Abraham) only works if you are within main method. If not, you can call System.exit(0) .

A few things. First of all, if you want to do something like:

while (condition == true){
    //do stuff
}

Then define a boolean function:

public static bool test(int cube[][]){
    return (cube[5][0] == 5 && cube[5][2] == 5 && cube[3][8] == 5 && cube[5][1] == 5 && cube[5][3] == 5 && cube[5][5] == 5);
}

You probably don't need try-catch; there is no exception being thrown by checking an array unless you are trying to read values that are beyond its boundaries. Instead, just run some code to confirm that the range you are testing exists in the cube:

public static void main(String[] args){
    while(test(myCubeInstance)){
        //do stuff
    }

public static bool test(int cube[][]){
    if existsInCube(5, 5){ //if cube has 5 entries in each dimension
        return (cube[5][0] == 5 && cube[5][2] == 5 && cube[3][8] == 5 && cube[5][1] == 5 && cube[5][3] == 5 && cube[5][5] == 5);
    } else {
        return false;
    }
}

public static bool existsInCube(int row, int col){
    return cube.length >= row and cube[0].length >= col;
}

It should be relatively simple to rewrite the boolean functions if I've made a bad assumption about what you're doing. It's been a little while since I wrote in java, so feel free to point out any mistakes or errors I've made. I'll correct them.

To make it closer to your code, I'd do this:

public static void stop_running(int cube[][]){
    try {
        if(cube[5][0] == 5 && cube[5][2] == 5 && cube[3][8] == 5 && cube[5][1] == 5 && cube[5][3] == 5 && cube[5][5] == 5)

           //I suppose this if is the exit condition, isn't it? 
           throw new Exception("stop");

    } catch (Exception stop){
        System.out.println("CHECK");
        //If you want the program to exit here, then:
        System.exit(0);

    }
}

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