[英]Print a rectangle with diagonals using a 2d array in Java
我一直在嘗試修改打印到控制台的普通矩形2d數組,以顯示其對角線以及其他字符。 例如,我當前具有2d數組的矩形的代碼是:
import java.util.Scanner;
class RecArray {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Height: ");
int height = scanner.nextInt();
System.out.print("Width: ");
int width = scanner.nextInt();
char[][] square = new char[height][width];
String line;
// fill the array
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
square[i][j] = 'o';
}
}
// print the array
for (int i = 0; i < height; i++) {
line = "";
for (int j = 0; j < width; j++) {
line += square[i][j];
}
System.out.println(line);
}
}
}
它返回:
Height: 10
Width: 10
oooooooooo
oooooooooo
oooooooooo
oooooooooo
oooooooooo
oooooooooo
oooooooooo
oooooooooo
oooooooooo
oooooooooo
我希望對角線代碼返回:
Height: 5
Width: 7
xooooox
oxoooxo
ooxxxoo
oxoooxo
xooooox
我當前的代碼是:
import java.util.Scanner;
class RecArrayDiag {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Height: ");
int height = scanner.nextInt();
System.out.print("Width: ");
int width = scanner.nextInt();
char[][] square = new char[height][width];
boolean bool1 = true;
boolean bool2 = true;
boolean bool3 = true;
boolean bool4 = true;
String line;
int x = 0;
for (int i = 0; i < height; i++) {
for (int j = width-1; j >= 0; j--) {
if (i % 2 == 0 ? ((i == height/2)) : ((i == height-1/2))) {
bool1 = false;
}
if (j % 2 == 0 ? ((j == width/2)) : ((j == width-1/2))) {
bool2 = false;
}
if ((((i == j) && bool1 && bool2) || (i == (height - (j+1))) || (j == (width - (i+1))) || ((j == width-1) && bool3) || ((i == height-1) && bool4) || (j == width-1) && (i == height-1))) {
square[i][j] = 'x';
//x++;
} else {
square[i][j] = 'o';
}
if ((j == width-1)) {
bool3 = false;
}
if ((i == height-1)) {
bool4 = false;
}
}
x++;
}
// print the array
for (int i = 0; i < height; i++) {
line = "";
for (int j = 0; j < width; j++) {
line += square[i][j];
}
System.out.println(line);
}
}
}
並返回:
Height: 5
Width: 7
xoooxox
oxoxoxo
ooxoxoo
oxoxooo
xoxooox
請幫助我解決這個問題,並在此先感謝。
這是一種實現方法,可重用的方法將應用於矩形的各種操作分開。
public static void printRectangleWithDiagonals(int width, int height) {
char[][] rectangle = new char[height][width];
fill(rectangle, 'o');
drawDiagonals(rectangle, 'x');
print(rectangle);
}
private static void fill(char[][] rectangle, char ch) {
for (char[] line : rectangle)
for (int i = 0; i < line.length; i++)
line[i] = ch;
}
private static void drawDiagonals(char[][] rectangle, char ch) {
int bottom = rectangle.length - 1, right = rectangle[0].length - 1;
if (right > bottom) {
for (int x = 0; x <= right; x++) {
int y = (x * bottom + right / 2) / right;
rectangle[y][x] = ch;
rectangle[bottom - y][x] = ch;
}
} else {
for (int y = 0; y <= bottom; y++) {
int x = (y * right + bottom / 2) / bottom;
rectangle[y][x] = ch;
rectangle[y][right - x] = ch;
}
}
}
private static void print(char[][] rectangle) {
for (char[] line : rectangle)
System.out.println(line);
}
測試
printRectangleWithDiagonals(7, 7);
System.out.println();
printRectangleWithDiagonals(10, 4);
System.out.println();
printRectangleWithDiagonals(5, 9);
產量
xooooox
oxoooxo
ooxoxoo
oooxooo
ooxoxoo
oxoooxo
xooooox
xxooooooxx
ooxxxxxxoo
ooxxxxxxoo
xxooooooxx
xooox
oxoxo
oxoxo
ooxoo
ooxoo
oxoxo
oxoxo
xooox
xooox
據我了解,您想展示某種十字架。 您要處理矩陣不是正方形的情況。
這意味着您可以直接從所有角度到達中心點,如果一個軸到達數組的中間,則首先停止計數器並繼續第二個參數。
像這樣的東西(只是偽代碼):
//create square with "o" everywhere then overwrite
int i = 0;
int j = 0;
while(i < height/2 || j < width/2){
//go from all corners towards the middle
if (i == j){
square[i][j] = "x";
square[i][width - j+1] = "x";
square[height - i+1][j] = "x";
square[height - i+1][width - j+1] = "x";
} else if (i < height/2) { //i is in middle of array
square[i][j] = "x";
square[i][width - j+1] = "x";
} else { //j is is in middle of array
square[i][j] = "x";
square[height - i+1][j] = "x";
}
//as long i and j did not reach the center add 1
if (i < width/2) { i++ }
if (j < height/2) { j++ }
}
希望這個對你有幫助。 通常,我建議將您的問題分解為不同的部分。
我可以在您的解決方案中看到邏輯,但請嘗試使其保持簡單。 查找只要條件為真的規則。 (在這種情況下:只要您不在任何數組中間),然后嘗試為不正確的情況找到解決方案。 (例如,如果我到達數組的中間但j沒有,會發生什么)
這樣,您可以拆分代碼,使其更易於閱讀/維護。
在大多數情況下,如果您有大量的if else語句,則很有可能將它們重寫為較小的部分。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.