You don't have to write for twice when you make a right triangle in Java

Making a right triangle with java is often a problem for beginners How do you guys write?

Problem: Output ■ to make a right triangle

Example: When there are 5 ■ on the bottom ■ ■■ ■■■ ■■■■ ■■■■■ To be

Common answers

Triangle.java


class Triangle {
	public static void main(String[] args) {
		int n = 5;
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < i + 1; j++) {
				System.out.print("■");
			}
			System.out.println();
		}
	}
}

Execution result.java


■
■■
■■■
■■■■
■■■■■

I think this is a common answer I haven't seen much else

There is another solution

Triangle.java


class Triangle {
	public static void main(String[] args) {
		int n = 5;
		String str = "";
		for (int i = 0; i < n; i++) {
			str += "■";
			System.out.print(str + "\n");
		}
	}
}

Execution result.java


■
■■
■■■
■■■■
■■■■■

Avoided nesting of for The print method also requires only one call When studying for sentences, you can intentionally nest them to move your brain, but in actual situations you want to avoid nesting.

Postscript

It is faster to use StringBuilder than to add ■ with the + operator. Let's write as follows (Thank you for pointing out based on the code of @ saka1029 in the comment)

Triangle.java


class Triangle {
    public static void main(String[] args) {
		var n = 5;
		var sb = new StringBuilder();
		for (var i = 0; i < n; ++i)
		    System.out.println(sb.append("■"));
	}
}

By the way, writing the type as var is a writing method that can be used in JDK 10 or later. Reference: [JDK 10] Type inference using var

Recommended Posts

You don't have to write for twice when you make a right triangle in Java
A note when you want Tuple in Java
I wanted to make (a == 1 && a == 2 && a == 3) true in Java
[Java basics] Let's make a triangle with a for statement
I tried to make a login function in Java
When you want to dynamically replace Annotation in Java8
I don't know, so I'm going to write a list (you don't have to read it)
I just wanted to make a Reactive Property in Java
I tried to make a client of RESAS-API in Java
[Rails Tutorial Chapter 2] What to do when you make a mistake in the column name
How to make a Java container
How to make a Java array
How to make a groundbreaking diamond using Java for statement wwww
When you want Rails to disable a session for a specific controller only
How to pass a proxy when throwing REST over SSL in Java
I tried to make a talk application in Java using AI "A3RT"
[Java] [For beginners] How to insert elements directly in a 2D array
How to make a Java calendar Summary
When seeking multiple in a Java array
Easy to make Slack Bot in Java
[Introduction to Java] How to write a Java program
How to make a Discord bot (Java)
When I wanted to create a method for Premium Friday, it was already in the Java 8 standard API
[Java] Eclipse shortcuts that make sense when you go back and forth between source code in a project
What to do when you think you can't do Groovy-> Java in IntelliJ IDEA CE
What to do if you have installed Java for OS X on macOS
How to make a judgment method to search for an arbitrary character in an array
[Rails] How to write when making a subquery
How to display a web page in Java
I did Java to make (a == 1 && a == 2 && a == 3) always true
Try to create a bulletin board in Java
When you want to bind InputStream in JDBI3
[Java] When putting a character string in the case of a switch statement, it is necessary to make it a constant expression
How to make a lightweight JRE for distribution
Make a snippet for Thymeleaf in VS Code
How to make a follow function in Rails
How to write Java String # getBytes in Kotlin?
Things to watch out for in Java equals
[Java] How to make multiple for loops single
How to make a Vagrant Plugin that you learned when you forked and published vagrant-mutagen
Guidelines for writing processing when a value exists / does not exist in Java Optional
A note for when someone who was Java Java until yesterday came to touch Scala
What to do if you get a NoClassDefFoundError when trying to run eclipse on Java9
A study method for inexperienced people to pass Java SE 8 Silver in one month
[Android studio / Java] What you don't understand when you touch it for the first time
Code to use when you want to process Json with only standard library in Java
How to write a unit test for Spring Boot 2
Write a class that can be ordered in Java
Initialization of for Try to make Java problem TypeScript 5-4
java: How to write a generic type list [Note]
Things to watch out for when creating a framework
Write a class in Kotlin and call it in Java
Things to watch out for in future Java development
I tried to create a Clova skill in Java
[Personal memo] Make a simple deep copy in Java
How to create a data URI (base64) in Java
A note for Initializing Fields in the Java tutorial
[For beginners] Minimum sample to display RecyclerView in Java
How to write a date comparison search in Rails
What I learned when building a server in Java
How to convert A to a and a to A using AND and OR in Java