At the brain teaser level, I made a code to draw a simple pattern. Originally created as an assignment for newcomers at work, it may be a bit of a test for Java beginners.
*.*.*.*.*.
.*.*.*.*.*
*.*.*.*.*.
.*.*.*.*.*
*.*.*.*.*.
.*.*.*.*.*
*.*.*.*.*.
.*.*.*.*.*
*.*.*.*.*.
.*.*.*.*.*
/**
*Draw a fine checkered pattern (check).
* @param outputSize Drawing size (number of digits).
*/
public static void printFineCheckered(int outputSize) {
//Move in the row direction
for (int i=0; i<outputSize; i++) {
//Move in the column direction
for (int j=0; j<outputSize; j++) {
if ((i+j)%2==0) {
System.out.print("*");
} else {
System.out.print(".");
}
}
//Insert a line break
System.out.print(System.lineSeparator());
}
}
*........*
.*......*.
..*....*..
...*..*...
....**....
....**....
...*..*...
..*....*..
.*......*.
*........*
/**
*Draw a cross.
* @param outputSize Drawing size (number of digits).
*/
public static void printCrossMark(int outputSize) {
//Move in the row direction
for (int i=0; i<outputSize; i++) {
//Move in the column direction
for (int j=0; j<outputSize; j++) {
if (i==j) {
//A line extending from the upper left to the lower right
System.out.print("*");
} else if (i+j==outputSize-1) {
//A line extending from the lower left to the upper right
System.out.print("*");
} else {
System.out.print(".");
}
}
//Insert a line break
System.out.print(System.lineSeparator());
}
}
**..**..**
**..**..**
..**..**..
..**..**..
**..**..**
**..**..**
..**..**..
..**..**..
**..**..**
**..**..**
/**
*Draw a checkerboard pattern.
* @param outputSize Drawing size (number of digits).
*/
public static void printIchimatsu(int outputSize) {
//Move in the row direction
for (int i=0; i<outputSize; i++) {
//Move in the column direction
for (int j=0; j<outputSize; j++) {
int rowIndex = i%4;
int colIndex = j%4;
if (rowIndex<2 && colIndex<2) {
System.out.print("*");
} else if (rowIndex>=2 && colIndex>=2) {
System.out.print("*");
} else {
System.out.print(".");
}
}
//Insert a line break
System.out.print(System.lineSeparator());
}
}
Recommended Posts