0
我使用地形表示法制作柵欄,其中1是圍欄土地,0是空的空間。這裏將是例如地圖:使用地形表示法的柵欄的中間部分
代碼是如何工作的是它會掃描角和1的側面和計數多少您需要的米數的柵欄,其中1的每邊爲2.5米。 j變量就像x值,而i是y值。我有邊和邊角的工作代碼,我需要幫助的唯一的事情是我如何去設置for循環的中心柵欄。這是我的工作代碼:
package workspace;
public class Fencing {
public static void main(String[] args) {
boolean b = true;
double corner = 0;
double center = 0;
double side = 0;
int i = 0;
int j = 0;
int[][] map = {
{ 0, 1, 1, 0 },
{ 1, 1, 1, 1 },
{ 1, 1, 1, 1 },
{ 1, 1, 0, 1 }
};
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
if (map[i][j] != 1 && map[i][j] != 0) {
b = false;
System.out.println("--> A value of " + map[i][j] + " was found at " + i + "," + j);
}
}
}
if (b == false && i == 3) {
System.out.println("The map is invalid");
return;
} else {
System.out.println("Map is valid");
}
if (map[0][0] == 1) {
corner += 5;
if (map[1][0] == 1 && map[0][1] == 1) {
corner += 0;
}
if (map[1][0] != 1 && map[0][1] != 1) {
corner += 5;
}
if (map[1][0] != 1 && map[0][1] == 1 || map[1][0] == 1 && map[0][1] != 1) {
corner += 2.5;
}
} else {
corner += 0;
}
if (map[0][3] == 1) {
corner += 5;
if (map[1][3] == 1 && map[0][2] == 1) {
corner += 0;
}
if (map[1][3] != 1 && map[0][2] != 1) {
corner += 5;
}
if (map[1][3] != 1 && map[0][2] == 1 || map[1][3] == 1 && map[0][2] != 1) {
corner += 2.5;
}
} else {
corner += 0;
}
if (map[3][0] == 1) {
corner += 5;
if (map[3][1] == 1 && map[2][0] == 1) {
corner += 0;
}
if (map[3][1] != 1 && map[02][0] != 1) {
corner += 5;
}
if (map[3][1] != 1 && map[2][0] == 1 || map[3][1] == 1 && map[2][0] != 1) {
corner += 2.5;
}
} else {
corner += 0;
}
if (map[3][3] == 1) {
corner += 5;
if (map[3][2] == 1 && map[2][3] == 1) {
corner += 0;
}
if (map[3][2] != 1 && map[2][3] != 1) {
corner += 5;
}
if (map[3][2] != 1 && map[2][3] == 1 || map[3][2] == 1 && map[2][3] != 1) {
corner += 2.5;
}
} else {
corner += 0;
}
System.out.println("Corner fencing = " + corner);
i = 0;
j = 0;
for (j = 1; j < 3; j++) { // Top
if (map[0][j] == 1) {
side += 2.5;
if (map[0][j + 1] == 1 && map[0][j - 1] == 1) {
side += 0;
}
if (map[0][j + 1] != 1 || map[0][j - 1] != 1) {
side += 2.5;
}
if (map[0][j + 1] != 1 && map[0][j - 1] != 1) {
side += 5;
}
} else {
side += 0;
}
}
System.out.println("After the first row, fencing = " + side);
i = 0;
j = 0;
for (j = 1; j < 3; j++) { // Bottom
if (map[3][j] == 1) {
side += 2.5;
if (map[3][j + 1] == 1 && map[3][j - 1] == 1) {
side += 0;
}
if (map[3][j + 1] != 1 || map[3][j - 1] != 1) {
side += 2.5;
}
if (map[3][j + 1] != 1 && map[3][j - 1] != 1) {
side += 5;
}
} else {
side += 0;
}
}
System.out.println("After the bottom row, fencing = " + side);
i = 0;
j = 0;
for (i = 1; i < 3; i++) {// Right
if (map[i][3] == 1) {
side += 2.5;
if (map[i + 1][3] == 1 && map[i - 1][3] == 1) {
side += 0;
}
if (map[i + 1][3] != 1 || map[i - 1][3] != 1) {
side += 2.5;
}
if (map[i + 1][3] != 1 && map[i - 1][3] != 1) {
side += 5;
}
} else {
side += 0;
}
}
System.out.println("After the first column, fencing = " + side);
i = 0;
j = 0;
for (i = 1; i < 3; i++) {// Left
if (map[i][0] == 1) {
side += 2.5;
if (map[i + 1][0] == 1 && map[i - 1][0] == 1) {
side += 0;
}
if (map[i + 1][0] != 1 || map[i - 1][0] != 1) {
side += 2.5;
}
if (map[i + 1][0] != 1 && map[i - 1][0] != 1) {
side += 5;
}
} else {
side += 0;
}
}
System.out.println("After the last column, fencing = " + side);
}
}
我不確定如果我甚至會用一個for循環掃描的核心,但我想它會是這樣的一側循環。任何人都有關於代碼的樣子的想法?
該示例的期望輸出是什麼? –
對於我提供的某個映射(與陣列中的映射相同),它將爲2.5,因爲中心只有一個1位於旁邊0 –
如果您在codereview.stackexchange處提問此問題.com,他們會提出一個更清晰的寫作方式,使它更短,更易於理解。你有很多複製粘貼的事情,每當你複製粘貼代碼時,都有更好的方式來做事。 – tucuxi