[Java] Draw a simple pattern

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)

Output image

*.*.*.*.*.
.*.*.*.*.*
*.*.*.*.*.
.*.*.*.*.*
*.*.*.*.*.
.*.*.*.*.*
*.*.*.*.*.
.*.*.*.*.*
*.*.*.*.*.
.*.*.*.*.*

code

/**
 *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

Output image

*........*
.*......*.
..*....*..
...*..*...
....**....
....**....
...*..*...
..*....*..
.*......*.
*........*

code

/**
 *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 checkered pattern (check)

Output image

**..**..**
**..**..**
..**..**..
..**..**..
**..**..**
**..**..**
..**..**..
..**..**..
**..**..**
**..**..**

code

/**
 *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

[Java] Draw a simple pattern
3 Implement a simple interpreter in Java
[Java] Strategy pattern
Java design pattern
java callback pattern
[Java] Singleton pattern
[Java] Adapter pattern
Java pattern memo
Create a simple bulletin board with Java + MySQL
java I tried to break a simple block
A collection of simple questions for Java beginners
CICS-Run Java application-(1) Run a simple sample app
java build a triangle
Simple htmlspecialchars in Java
[Swift 5] Draw a line.
Builder pattern (Effective Java)
Java Lambda Command Pattern
Java design pattern summary
I made a simple calculation problem game in Java
Let's make a robot! "A simple demo of Java AWT Robot"
Docker × Java Building a development environment that is too simple
How to deploy a simple Java Servlet app on Heroku
[Beginner] Try to make a simple RPG game with Java ①
[Design pattern] Java core library
[Java] Create a temporary file
Draw a gradient with CAGradientLayer
What is a Java collection?
[Java] Make it a constant
Enum Strategy pattern in Java
2 Implement simple parsing in Java
Java creates a Word document
A Simple CRUD Sample Using Java Servlet / JSP and MySQL
Make a rhombus using Java
Create a simple DRUD application with Java + SpringBoot + Gradle + thymeleaf (1)
Create a simple web server with the Java standard library com.sun.net.httpserver
Think of a Java update strategy
Build a Java project with Gradle
Very simple input reception in Java
Make a language! (Making a simple calculator ②)
[Question] Draw a diamond in a rectangle
I created a PDF in Java.
I made a shopify app @java
How to make a Java container
A person writing C ++ tried writing Java
A really scary (Java anti-pattern) story
Sort a List of Java objects
Run a batch file from Java
Create a Java project using Eclipse
[Java] How to create a folder
Make a language! (Making a simple calculator ①)
I made a simple recommendation function.
Draw a pie chart with Chart.js
[Android] Simple Date Format format pattern list
JNA (Java Native Access) pattern collection
A brief description of JAVA dependencies
Java Calendar is not a singleton.
What is a lambda expression (Java)
Get stuck in a Java primer
1 Implement simple lexical analysis in Java
Java beginner design pattern (Factory Method pattern)
How to make a Java array