gpt4 book ai didi

java - 无法使用另一个类正确访问数组

转载 作者:行者123 更新时间:2023-11-30 04:08:08 25 4
gpt4 key购买 nike

我正在尝试获取在类 LightsOutPuzzle 类中初始化的 outputArray 值,求解。但是在打印数组的元素之后

1) 在LightsOutPuzzle中调用类解决之前 2)调用solve后,元素不相等,就像重新初始化了一样。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.StringTokenizer;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class LightsOutPuzzle extends JFrame implements ActionListener {

public JFrame f;
public JPanel p1;
public JPanel p2;
public JButton[] buttonArray;
public JButton solve;
public int nRows = 5;
public int nColumns = 5;
public int nTotalButtons = nRows * nColumns;
public int[] outputArray = new int[25];

public void create() {
init();
gui();
}

public void gui() {
f = new JFrame("Lights Out Puzzle");
f.setVisible(true);
f.setSize(500,500);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

p2 = new JPanel();

solve = new JButton("Solve");

p2.add(solve);

f.add(p1);
f.add(p2, BorderLayout.SOUTH);

solve.addActionListener(new Action());
}

public void init() {
p1 = new JPanel();
p1.setLayout(new GridLayout(nRows, nColumns, 0, 0));

int[] inputArray = new int[25];
int i=0;

buttonArray = new JButton[nTotalButtons];
for (int nNum = 0; nNum < nTotalButtons; nNum++)
{
buttonArray[nNum] = new JButton("");
add(buttonArray[nNum]);
//clickability
buttonArray[nNum].addActionListener(this);
buttonArray[nNum].setBackground(Color.yellow);
buttonArray[nNum].setActionCommand("" + nNum);
}

//reading config file
BufferedReader br = null;
try {
String line;
br = new BufferedReader(new FileReader("input.txt"));

while ((line = br.readLine()) != null) {
StringTokenizer stringTokenizer = new StringTokenizer(line, " ");
while (stringTokenizer.hasMoreElements()) {
inputArray[i] = Integer.parseInt(stringTokenizer.nextElement().toString());
i++;
}
}

} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();

} catch (IOException ex) {
ex.printStackTrace();
}
}

//drawing text input
for(i=0; i<nTotalButtons; i++){
if(inputArray[i] == 1)
buttonArray[i].setBackground(Color.white);
p1.add(buttonArray[i]);
}
}

public void save() {
int i = 0;
for (i=0; i<25; i++) {
if(buttonArray[i].getBackground() == Color.yellow)
outputArray[i] = 1;
else
outputArray[i] = 0;
}
}

@Override
public void actionPerformed(ActionEvent e)
{
//conditions for the puzzle
}

public class Action implements ActionListener {
public void actionPerformed (ActionEvent e) {
int i;

save();
for(i=0; i<25; i++)
System.out.println(outputArray[i]);
new solve();
}
}

public static void main(String[] args) {
LightsOutPuzzle lop = new LightsOutPuzzle();
lop.create();
}
}

此时,outputArray 的所有元素都等于 1。但是,当我使用其他类解决时打印它们,outputArray 的所有元素都为现在等于 0。

import java.awt.*; 
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;

public class solve extends LightsOutPuzzle {
private JFrame f;
private JPanel p1;

int[] inputArray = new int[25];
int[] zeroArray = new int[25];
int[] UIState = new int[25];
int[] boardState = new int[25];
Queue<int[]> queue = new LinkedList<int[]>();

public solve() {
gui();
init();
}

public void gui() {
f = new JFrame("Solution");
f.setVisible(true);
f.setSize(500,500);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

p1 = new JPanel();
p1.setLayout(new GridLayout(nRows, nColumns, 0, 0));

buttonArray = new JButton[nTotalButtons];
for (int nNum = 0; nNum < nTotalButtons; nNum++) {
buttonArray[nNum] = new JButton("");
add(buttonArray[nNum]);
//buttonArray[nNum].setBackground(Color.white);
buttonArray[nNum].setActionCommand("" + nNum);
p1.add(buttonArray[nNum]);
}

f.add(p1);
}

public void init() {
//drawing text input
int i;
for(i=0; i<25; i++)
System.out.println(outputArray[i]);
for(i=0; i<nTotalButtons; i++){
if(outputArray[i] == 1)
buttonArray[i].setBackground(Color.yellow);
}
}

public static void main(String[] args) {
}

}

我还希望了解有关 Java 正确编程实践的提示。我已经很长时间没有用这种语言编写代码了,所以我只是尝试根据我的内存来创建一个程序..

最佳答案

尽管 solve 类扩展了 LightsOutPuzzle,但它是一个不同的对象,因此拥有自己的 outputArray 副本。在代码的第一部分中,您将打印 LightsOutPuzzle 中的 outputArray 实例,该实例已初始化为 1:

LightsOutPuzzle 中的 ActionListener

    public class Action implements ActionListener {
public void actionPerformed (ActionEvent e) {
int i;

save();
for(i=0; i<25; i++)
{
/////////////PRINTS OUT outputArray IN LightsOutPuzzle
System.out.println(outputArray[i]);
}

////CREATES NEW solve OBJECT WITH ITS OWN outputArray COPY
new solve();
}
}

然后,当您调用 solve 构造函数时,您将打印尚未初始化且全为零的 solve 副本:

解决类中的初始化方法

public void init() {
//drawing text input
int i;
for(i=0; i<25; i++)
{
//////PRINTS OUT outputArray IN solve CLASS
System.out.println(outputArray[i]);
}
for(i=0; i<nTotalButtons; i++){
if(outputArray[i] == 1)
buttonArray[i].setBackground(Color.yellow);
}
}

关于java - 无法使用另一个类正确访问数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20269866/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com