【发布时间】:2021-07-04 19:28:59
【问题描述】:
所以这个问题实际上是与作业相关的 2 合 1 问题。我必须编写一个程序,将包含土地、水、沙袋和房屋瓷砖的文件中的地图读取到二维数组和显示地图的 GUI 中。
按下按钮时,水砖会散布到所有接触它的土地和房屋瓷砖(水平、垂直和对角线),然后显示一条消息,告诉您房屋是否被淹。
这必须通过递归方法来完成,我有一个程序在很大程度上使用 8 个 if 语句,我认为这些语句效率非常低,而且也不真正符合分配要求。我怎样才能使这个递归?
我认为与我处理洪水问题的方式有关的第二个问题是,我检查房子是否仍然站立的方法总是显示房屋被淹消息,即使房子是站立的。
我已经尝试解决这个问题好几天了,任何见解都将不胜感激。 (我以前也遇到过一个问题,在我的for 方法中使用for 循环会导致我的程序冻结并且必须通过任务管理器关闭,即使我没有更改任何东西,它也会随机停止这样做不确定是否这有帮助)。
public static void Flood(JLabel[][] labelArray, JPanel panel) {
//Timer that allows the GUI to update slowly so progression can be seen
ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
boolean running = true;
while(running) {
//Checks through the entire array to find water tiles
for (int i = 0; i < labelArray.length; i++) {
for (int j = 0; j < labelArray[0].length; j++) {
if (labelArray[i][j].getText().equals("W")) {
if(i > 0 && (labelArray[i-1][j].getText().equals("L") || labelArray[i-1][j].getText().equals("H"))) {//Checks the tile left of the water tile
labelArray[i-1][j].setText("W");
labelArray[i-1][j].setBackground(Color.BLUE);
}
else if(j > 0 && (labelArray[i][j-1].getText().equals("L") ||labelArray[i][j-1].getText().equals("H"))) {//Checks the tile above the water tile
labelArray[i][j-1].setText("W");
labelArray[i][j-1].setBackground(Color.BLUE);
}
else if(i < labelArray.length-1 && (labelArray[i+1][j].getText().equals("L") || labelArray[i+1][j].getText().equals("H"))){//Checks the tile to the right of the water tile
labelArray[i+1][j].setText("W");
labelArray[i+1][j].setBackground(Color.BLUE);
}
else if(j < labelArray[0].length-1 && (labelArray[i][j+1].getText().equals("L") || labelArray[i][j+1].getText().equals("H"))) {//Checks the tile below the water tile
labelArray[i][j+1].setText("W");
labelArray[i][j+1].setBackground(Color.BLUE);
}
else if(j < labelArray[0].length-1 && i < labelArray.length-1 && (labelArray[i+1][j+1].getText().equals("L") || labelArray[i+1][j+1].getText().equals("H"))) {//Checks the tile to the bottom right of the water tile
labelArray[i+1][j+1].setText("W");
labelArray[i+1][j+1].setBackground(Color.BLUE);
}
else if(j > 0 && i > 0 && (labelArray[i-1][j-1].getText().equals("L") || labelArray[i-1][j-1].getText().equals("H"))) {//Checks the tile to the top left of the water tile
labelArray[i-1][j-1].setText("W");
labelArray[i-1][j-1].setBackground(Color.BLUE);
}
else if(j > 0 && i < labelArray.length-1 && (labelArray[i+1][j-1].getText().equals("L") || labelArray[i+1][j-1].getText().equals("H"))) {//Checks the tile to the top right of the water tile
labelArray[i+1][j-1].setText("W");
labelArray[i+1][j-1].setBackground(Color.BLUE);
}
else if(j < labelArray[0].length-1 && i > 0 && (labelArray[i-1][j+1].getText().equals("L") || labelArray[i-1][j+1].getText().equals("H"))) {//Checks the tile to the bottom left of the water tile
labelArray[i-1][j+1].setText("W");
labelArray[i-1][j+1].setBackground(Color.BLUE);
}
}
}
}
running = false;
checkForHouse(labelArray);
}
}
};
new Timer(250, taskPerformer).start();
}
public static void checkForHouse (JLabel[][] labelArray) {
for (int r =0; r<labelArray.length; r++) {
for (int c =0;c<labelArray[0].length; c++) {
if(labelArray[r][c].getText().equals("H")) {
textArea.setText(safeHouse);
}
else {
textArea.setText(floodedHouse);
}
}
}
}
【问题讨论】:
标签: java swing if-statement recursion multidimensional-array