Static variable cannot be modified

Asked 2 years ago, Updated 2 years ago, 65 views

Full code.

package recursion;
public class study09 {
    private static int N = 8;
    static int[] cols = new int[N];

    static boolean queens(int level) {
        System.out.println ("queens call / level = " + level");
        For (inti = 0; i < N; i++) {// For moving columns
            System.out.println("1");
            cols[level] = i;// Specify Queen Position
            System.out.println("2");
            If (level!= 0) {// The initial location is specified as No object to compare
                System.out.println("3");
                boolean position = true;
                for (int j = 0; j < level; j++) {
                    System.out.println("4");
                    for (int k = 0; k < cols.length; k++) {
                        System.out.println("cols[" + k + "] = " + cols[k]);
                    }
                    if (cols[level] == cols[j]) {
                        System.out.println("5");
                        position = false;
                        break;
                    }
                    System.out.println(Math.abs(level - j) + ", " + Math.abs(cols[level] - cols[j]));
                    if (Math.abs(level - j) == Math.abs(cols[level] - cols[j])) {
                        System.out.println("6");
                        position = false;
                        break;
                    }

                }
                if (position == false) {
                    continue;
                }
                if (level == N - 1) {
                    System.out.println("7");
                    for (int k = 0; k < cols.length; k++) {
                        System.out.println("8");
                        System.out.println("cols[" + k + "] = " + cols[k]);
                    }
                    return true;
                }
                if (queens(level + 1)) {
                    System.out.println("9");
                    return true;
                }
            } else {// level 0 (first)
                if (level == N - 1) {
                    System.out.println("10");
                    return true;
                }
                if (queens(level + 1)) {
                    System.out.println("11");
                    return true;
                }
            }
        }
        return false;
    }

    public static void main(String[] args) {
        System.out.println ("Start Main");
        for (int i = 0; i < cols.length; i++) {
            study09.cols[i] = -1;
        }
        for (int i = 0; i < cols.length; i++) {
            System.out.println(study09.cols[i]);
        }
        boolean result = queens(0);
    }
}

Before running queens(0) on the main

System.out.println ("Start Main");
        for (int i = 0; i < cols.length; i++) {
            study09.cols[i] = -1;
        }
        for (int i = 0; i < cols.length; i++) {
            System.out.println(study09.cols[i]);
        }

This phrase is ignored. Please explain why it doesn't work.

static

2022-09-21 11:17

1 Answers

How did you determine that the phrase was not implemented? I turned it around and there's no problem.

Perhaps because of the log exceeding the console buffer, the log at the beginning was blown away, so I can't see the log "Start Main", so I think I decided that the syntax was not executed either.


2022-09-21 11:17

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.