Java 在“扫雷舰”中更改父类变量时遇到问题

Java 在“扫雷舰”中更改父类变量时遇到问题,java,Java,在父类中有三个变量可以改变游戏的难度 public class MineSweeper implements ActionListener{ int gridlength = 20; int gridheight = 20; int numMines = 30; 当用户单击“新建游戏”时,会出现提示,允许用户更改难度。它确实运行以下提示,但始终恢复为上述值。如何更改新游戏的值 public void actionPerformed(ActionEvent e)

在父类中有三个变量可以改变游戏的难度

public class MineSweeper implements ActionListener{
    int gridlength = 20;
    int gridheight = 20;    
    int numMines = 30;
当用户单击“新建游戏”时,会出现提示,允许用户更改难度。它确实运行以下提示,但始终恢复为上述值。如何更改新游戏的值

public void actionPerformed(ActionEvent e) {
    if (e.getSource() == newGame){
        String gridl = JOptionPane.showInputDialog(subframe, "Please enter in the amount of columns.");
        gridlength = Integer.parseInt(gridl);
        String gridh = JOptionPane.showInputDialog(subframe, "Please enter in the amount of rows.");
        gridheight = Integer.parseInt(gridh);
        String amountOfMines = JOptionPane.showInputDialog(subframe, "Please enter in the amount of mines.");   
        numMines = Integer.parseInt(amountOfMines);
        new MineSweeper();

您正在用全新的默认值实例化一个完全不同的
扫雷舰
;这就是问题所在你确定要使用递归吗?为GUI提供一个
reset()
方法,您可以在更改上述字段后调用该方法,然后允许它重置其所有值并重新绘制GUI,这不是更好吗?对不起,伙计们!!我才恍然大悟。我必须将父类中的变量更改为static int numines。。。。在newgame方法中,将变量声明更改为mineswipper.numMines=…不,不要将变量设置为静态,因为这是一个非常非OOP的解决方案,可能会使您的程序以后难以改进。如果我是您的讲师,我会降低您的分数,因为这是一种糟糕的编码技术。而是更改字段并重新构建网格。