1

我正在尝试在 java中实现A* 算法。要求用户键入要成为网格的宽度和高度。我的问题是验证该输入。这是我的代码:

public class Map extends java.awt.Panel implements Serializable
{
JFrame frame = new JFrame();
String rows =  JOptionPane.showInputDialog(frame,
                "Συμπληρώστε τον αριθμό γραμμών του πλέγματος: \n",
                "Δημιουργία πλέγματος",
                JOptionPane.PLAIN_MESSAGE);
String cols =  JOptionPane.showInputDialog(frame,
                "Συμπληρώστε τον αριθμό στηλών του πλέγματος: \n",
                "Δημιουργία πλέγματος",
                JOptionPane.PLAIN_MESSAGE);

int rowsnum = Integer.parseInt(rows); 
int colsnum = Integer.parseInt(cols);
GridCell gridCell[][] = new GridCell[rowsnum][colsnum];

public Map()
{
        super();

    setLayout(new GridLayout(rowsnum,colsnum)); 
    for(int i=0;i<rowsnum;i++){ 
        for(int j=0;j<colsnum;j++){
           gridCell[i][j] = new GridCell();
           gridCell[i][j].setPosition(new Point(i,j));
           add(gridCell[i][j]);
        }
    }
}

我试图用一种新方法检查输入,但我的问题是我需要访问rowsnum,colsnumgridcell[]我的程序的其他类。

我是 Java 新手,非常感谢任何帮助。:-)

4

3 回答 3

1

现在,您的 rowsnum、colsnum 和 GridCells 是“Map”类中的变量。

为了从其他类中将其作为实例变量访问,

public int rowsnum = // something
public int colsnum = // something
public GridCell girdCell[][] = //something

“public”表示可以从类外部访问变量。

在您的 Map 类之外,

Map m = new Map();
int rowsnum = m.rowsnum
int colsnum = m.colsnum
// access instance variables

但是,最好为实例变量创建 getter 和 setter。

也就是说,为获取和设置这些变量创建单独的方法。

public int getRowsNum() {
    return rowsnum;
}

等等。

于 2013-10-08T09:29:14.563 回答
1

您可以通过 Map 类对象访问它们(将访问修饰符更改为这些变量的 public )

示例:在 B 类中

public class Test {
    public static void  main(String args[]){
        Map someMap = new Map();

        //Access gridCell 
        someMap.gridCell;

        //Access rowsnum
        someMap.rowsnum;

        //Access colsnum
        someMap.colsnum;
    }
}

或者您可以将它们设为静态并使用类名访问它们

示例:Map.rowsnum;

于 2013-10-08T09:31:42.110 回答
0

我能想到的最简单的解决方案是

使用 If 验证宽度/高度数字

public class Map extends java.awt.Panel implements Serializable
    {      
    String width =  JOptionPane.showInputDialog("Give Width (>5 , <30)");
    int w = Integer.parseInt(width);

    String height =  JOptionPane.showInputDialog("Give height (>5 , <30)");
    int h = Integer.parseInt(height);

    GridCell gridCell[][] = new GridCell[w][h];

    public Map()
    {
    super();
    setLayout(new GridLayout(w,h)); 

    if ( w < 5)
    {JOptionPane.showMessageDialog ( null, "Number out of Limits ,application closing" );
     System.exit(0);}
    if ( w > 30)
    {JOptionPane.showMessageDialog ( null, "Number out of Limits ,application closing" );
     System.exit(0);}
    if ( h < 5)
    {JOptionPane.showMessageDialog ( null, "Number out of Limits ,application closing" );
     System.exit(0);}
    if ( h > 30)
    {JOptionPane.showMessageDialog ( null, "Number out of Limits ,application closing" );
     System.exit(0);}


        for(int i=0;i<w;i++){ 

            for(int j=0;j<h;j++){
               gridCell[i][j] = new GridCell();
               gridCell[i][j].setPosition(new Point(i,j));
               add(gridCell[i][j]);
            }
        }
    }

在公共主服务器上使用 try-catch 验证字符串输入错误

public static void main(String[] argv) {

try{    
AStar2 test = new AStar2();                   
test.setLocationRelativeTo(null);
test.setDefaultCloseOperation(test.EXIT_ON_CLOSE);}
catch(NumberFormatException e){JOptionPane.showMessageDialog ( null, "BLOODY HELL DONT PUT STRINGS FOR INPUT, APPLICATION IS CLOSING , TRY BETTER NEXT TIME" );}

    }

}
于 2013-10-12T11:12:34.937 回答