全局变量在函数中提供值后失去其值。无法在该函数之外访问其值

时间:2018-12-22 07:51:59

标签: java netbeans

实际上,我声明了一个具有初始值“ lo”的全局变量“ jjj”字符串类型,我在函数prant()中提供了它的值,但无法在jButton6ActionPerformed代码中的函数prant()之外访问其值。

import javax.swing.*;

/**
 *
 * @author prashant
 */
public class Avg_Game extends javax.swing.JFrame {

    public Avg_Game() {

        initComponents();
    }
    public  Avg_Game(String Om){
        pop = Om;
        initComponents();
    }
    String pop;
    JFrame frame = new JFrame();
    public String jjj = "lo";

    int[] mat[];
    int[] mat_sol[];
    int n,sqn,k;

    public Avg_Game(int n,int k) {
               /// String jjj;
        this.n = n;
        this.k = k;
                int m;
        Double sqnd = Math.sqrt(n);
        sqn = sqnd.intValue();
        mat = new int[n][n];
        mat_sol = new int[n][n];
               initComponents();
        }

    private void fillValue() {
        for(int i = 0;i < n;i = i + sqn) {
            fillBox(i,i);
            }
        fillRemaining(0,sqn);
        for(int j = 0;j < n;j++) {
            for(int k = 0;k < n;k++) {
                mat_sol[j][k] = mat[j][k];
            }
        }
               // this.mat = mat;
        removeKDigits();        //remove randomly k digits.
                //jTextField26.setText(mat[1][0]);
        }

    boolean unUsedInBox(int rowstart,int colstart,int num) {        //rreturn false if given 3*3 box contains num
        for(int i = 0;i<sqn;i++) {
            for(int j = 0;j<sqn;j++) {
                if(mat[rowstart + i][colstart + j] == num) {
                    return false;
                    }
                }
            }
        return true;
        }

    void fillBox(int row,int col) {
        int num;
        for(int i = 0;i<sqn;i++){
            for(int j = 0;j<sqn;j++) {
                do {
                    num = (int) Math.floor((Math.random()*n+1));
                }
                while(!unUsedInBox(row,col,num));
                mat[row + i][col + j] = num;
                }
            }
        }

    boolean checkIfSafe(int r,int c,int num) {
        for(int k = 0;k < n;k++) {
            if(mat[r][k] == num) {
                return false;
                }
            if(mat[k][c] == num) {
                return false;
                }
        }
        int rowstart = r - r%sqn;
        int colstart = c - c%sqn;
        for(int i = 0;i<sqn;i++) {
            for(int j = 0;j<sqn;j++) {
                if(mat[rowstart + i][colstart + j] == num) {
                    return false;
                    }
                }
            }

        return true;
        }

    boolean fillRemaining(int r,int c) {
        if (c>=n && r<n-1) { // j>=9 && i < 8
            r = r + 1; 
            c = 0; 
        } 
        if (r>=n && c>=n) // i >= 9 && j >= 9
            return true; 

        if (r < sqn) {  // i < 3 it is to skip the first diagonal 3x3 matrix
            if (c < sqn) // j< 3
                c = sqn; // j = 3
        } 
        else if (r < n-sqn)  {  // i < 6  it is to skip the second diagonal 3x3
            if (c==(int)(r/sqn)*sqn) // j == 3
                c =  c + sqn;   //  j =  6
        } 
        else { 
            if (c == n-sqn)  {  // j ==  6 && i < 9
                r = r + 1; 
                c = 0; 
                if (r>=n) 
                    return true; 
            } 
        } 

        for (int num = 1; num<=n; num++)  { 
            if (checkIfSafe(r, c, num))   { 
                mat[r][c] = num; 
                if (fillRemaining(r, c+1)) { 
                    return true;  }
                mat[r][c] = 0; 
            } 
        } 
        return false; 
     } 

    private void removeKDigits() {
        int count = k;
        while(count != 0) {
            int cellID = (int) Math.floor((Math.random()*(n*n)+1));
            int i = (cellID/n); 
            int j = cellID%n; 
         // if (j != 0) {
           //    j = j - 1; 
             // }
            if(i != n){
                if (mat[i][j] != 0) {
                 count--; 
                 mat[i][j] = 0; 
                  }
             } 
         }
    }


      //  jTextField8.setText(jjj);
    public void printSudoku()  { 
            for (int i = 0; i<n; i++) { 
                 for (int j = 0; j<n; j++) 
                         System.out.print(mat[i][j] + " "); 
                     System.out.println(); 
              } 
                jTextField1 = new JTextField();

             //   System.out.println(jjj);

               // try {
                //
              //  catch(Exception ex){}
            System.out.println(); 
            for (int i = 0; i<n; i++) { 
                for (int j = 0; j<n; j++) {
                    System.out.print(mat_sol[i][j] + " "); }
                     System.out.println(); 
              }
    }  

//编辑器折叠

    public void prant() {
        jjj = Integer.toString(mat[0][0]);
            System.out.println(jjj);
          // jTextField1.setText("loe");
        }

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {                                         
      System.out.println(jjj);     
    }                                        

    private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        dispose();
        xsudoku xs = new xsudoku(pop);
        xs.setVisible(true);
    }                                        

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        if(JOptionPane.showConfirmDialog(frame,"Confirm if you wanna to exit","wait wait wait!!!",JOptionPane.YES_NO_OPTION) == JOptionPane.YES_NO_OPTION) {
            System.exit(0);
        }
    }                                        

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        dispose();
        Home home = new Home();
        home.setVisible(true);
    }                                        

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */    
                int n = 9;
        int k = 40;
        int[][] mat = new int[n][n];
        Avg_Game sudoku = new Avg_Game(n,k);
        sudoku.fillValue();

        sudoku.printSudoku();
                 sudoku.prant(); 
             //   int j = sudoku.mat[0][0];
              //  System.out.println(j);
            //  sudoku.jTextField17.setText(Integer.toString(j));

        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Avg_Game().setVisible(true);
            }
        });
      //  sudoku.print();
      //  sudoku.jTextField8.setText("game");

    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JLabel jLMain;
    private javax.swing.JLabel jLTime;
    private javax.swing.JLabel jLTime1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField10;
    private javax.swing.JTextField jTextField11;
    private javax.swing.JTextField jTextField2;

}

1 个答案:

答案 0 :(得分:-1)

问题不是全局变量。请尝试以下操作:将方法jButton6ActionPerformed()公开,并在main()中调用prant()之后调用它,如下所示:

    sudoku.jButton6ActionPerformed(null);

这将正确打印更新后的变量。

我认为您遇到的问题是jButton6ActionPerformed即将被调用,或者您没有接到电话,也看不到预期的结果。

正如Andreas在评论中正确指出的那样,问题出在创建和显示表单时创建一个新实例,而不是使用相同的实例“ sudoku”。因此,替换以下内容可以解决此问题:

new Avg_Game().setVisible(true);

使用

sudoku.setVisible(true);