[Java basics] Let's make a triangle with a for statement

This is a problem that my senior at the company gave to me as a newcomer to make various triangles using for statements in Java. This is Java, but you can make it with a simple for statement, so please try other languages as well.

The shape to be handled is as shown in the image below.

triangles.PNG

Target audience

・ Those who are studying programming ・ Those who have recently become engineers ・ Those who want to study Java

Problem ① Equilateral triangle ▲

public class Main {
    public static void main(String[] args) {
        int max = 5;
        String star = "*";

        for (int i = 0; i < max; i++) {
            for(int j = 0; j < max-i; j++) {
                System.out.print(" ");
            }
            for(int k = 0; k <= (i*2); k++) {
                System.out.print(star);
            }
            System.out.println("\n");
       }
    }
}

Output result

image.png

Commentary

-Define the height of the triangle in the first for statement (i) (this time, the variable max = 5 is set in advance). -Define a blank ("") in the second for statement (j). Since it is a triangle, the blanks are the maximum at first, and the blanks are gradually reduced in inverse proportion to the number of *. The conditional expression is j <max-i ;. -Output * with the third for statement (k). -Finally, every time one loop ends, println will start a new line. (If you do not start a new line, it will be output horizontally all the time)

Problem ② Inverted triangle ▼

public class Main {
    public static void main(String[] args) {
        int max = 5;
        String star = "*";
    
        for (int i = 0; i < max; i++) {
            for(int j = 0; j < i; j++) {
                System.out.print(" ");
            }
            for(int k = 0; k <= max*2-(i*2)-2; k++){
                System.out.print(star);
            }
            System.out.println("\n");
        }
    }
}

Output result

image.png

Commentary

・ Define the height of the triangle in the first for statement (i) -Define a blank ("") in the second for statement (j). Unlike ①, this time I will simply increase it by 1. -Output * with the third for statement (k).

Problem ③ Right triangle ver.1 ◣

public class Main {
    public static void main(String[] args) {
     int max = 5;
        String star = "*";

        for (int i = 0; i < max; i++) {
            for (int j = 0; j <= i; j++) {
                System.out.print(star);
            }
            System.out.println("\n");
        }
    }
}

Output result

image.png

Commentary

This is probably the simplest.

・ Define the height of the triangle in the first for statement (i) -Output * so that it increases by one in the second for statement (j).

Problem ④ Right triangle ver.2 ◢

public class Main {
    public static void main(String[] args) {
        int max = 5;
        String star = "*";

        for (int i = 0; i < max; i++) {
            for (int j = 0; j < max-i; j++) {
                System.out.print(" ");
            }
            for (int k = 0; k <= i; k++){
                System.out.print(star);
            }
            System.out.println("\n");
        }
    }
}

Output result

image.png

Commentary

I think this is easy if you can make an inverted triangle.

・ Define the height of the triangle in the first for statement (i) -Define a blank ("") in the second for statement (j). At first, the blank is the maximum, and the number of blanks is gradually reduced in inverse proportion to the number of *. The conditional expression is j <max-i ;. -Output * with the third for statement (k). Since it is incremented one by one, k <= i.

Problem ⑤ Rhombus ◇

public class Main {
    public static void main(String[] args) {
        int max = 5;
        String star = "*";
    
        for (int i = 0; i < max; i++) {
            for (int j = 0; j < max-i; j++) {
                System.out.print(" ");
            }
            for (int k = 0; k <= ((i-1)*2); k++) {
                System.out.print(star);
            }
            System.out.println("\n");
            if (i == 4){
                for (int a = 0; a < max; a++) {
                    for(int b = 0; b < a; b++) {
                        System.out.print(" ");
                    }
                    for(int c = 0; c <= max*2-(a*2)-2; c++){
                        System.out.print(star);
                    }
                    System.out.println("\n");
                }
            }
        }
    }
}

Output result

image.png

Commentary

I thought about solving this for a while. As a result, use the if statement to create both equilateral and inverted triangles. I used the hand of outputting, but I think there is probably a better way ...

First, output from 1 to 7 * by the method of an equilateral triangle, and express by an inverted triangle from 9 to 1 *. Let's set if (i == 4) and switch between ▲ and ▼ when the i loop turns 4 times.

The rest can be solved by reusing the codes ① to ④.

Summary

I summarized how to output various shapes using the for statement. The for statement is frequent in practice, and it will be a practice to think about the structure of the code, so please try it (^^) /

Recommended Posts

[Java basics] Let's make a triangle with a for statement
Let's make a Christmas card with Processing!
Let's make a smart home with Ruby!
[Java] Let's make a DB access library!
Java for statement
How to make a groundbreaking diamond using Java for statement wwww
[Java] Let's create a mod for Minecraft 1.14.4 [Introduction]
[Java] Let's create a mod for Minecraft 1.16.1 [Introduction]
Let's make a search function with Rails (ransack)
[Java] Let's create a mod for Minecraft 1.14.4 [99. Mod output]
[Java] for statement, while statement
java build a triangle
(Memo) Java for statement
Let's scrape with Java! !!
Let's make a calculator application with Java ~ Create a display area in the window
[Java] Let's create a mod for Minecraft 1.14.4 [0. Basic file]
[Java] Let's create a mod for Minecraft 1.14.4 [4. Add tools]
[Java] Let's create a mod for Minecraft 1.14.4 [5. Add armor]
[Java] Let's create a mod for Minecraft 1.14.4 [Extra edition]
[Java] Let's create a mod for Minecraft 1.14.4 [7. Add progress]
[Java] Let's create a mod for Minecraft 1.14.4 [6. Add recipe]
Let's make a LINE Bot with Ruby + Sinatra --Part 2
[Java] Let's create a mod for Minecraft 1.16.1 [Add item]
[Java] Let's create a mod for Minecraft 1.16.1 [Basic file]
[Java] Let's create a mod for Minecraft 1.14.4 [1. Add items]
Let's make a robot! "A simple demo of Java AWT Robot"
Let's make a LINE Bot with Ruby + Sinatra --Part 1
[Java] Let's create a mod for Minecraft 1.14.4 [2. Add block]
[Java] Let's create a mod for Minecraft 1.16.1 [Add block]
You don't have to write for twice when you make a right triangle in Java
Let's experiment with Java inlining
[Java] Basic statement for beginners
Let's operate Excel with Java! !!
Java programming basics practice-for statement
[Java] Make it a constant
Java programming basics practice-switch statement
Getting Started with Java Basics
Make a rhombus using Java
Store in Java 2D map and turn with for statement
Deploying a Java environment with Windows Subsystem for Linux (WSL)
[Java] Make variables in extended for statement and for Each statement immutable
[Java] Let's create a mod for Minecraft 1.14.4 [3. Add creative tab]
Let's make a calculator application in Java ~ Display the application window
[Beginner] Try to make a simple RPG game with Java ①
I want to make a list with kotlin and java!
I want to make a function with kotlin and java!
Let's make a simple API with EC2 + RDS + Spring boot ①
[Java] Let's replace data objects with a mapper ~ BeanMapper Orika ~
Let's make a book management web application with Spring Boot part1
Make a digging maze with Ruby2D
Build a Java project with Gradle
Make a slideshow tool with JavaFX
How to make a Java container
Let's make a book management web application with Spring Boot part3
Make a Christmas tree with swift
Let's make a book management web application with Spring Boot part2
Enable OpenCV with java8. (For myself)
Make a garbage reminder with line-bot-sdk-java
Make a list map with LazyMap
[Java] Let's create a mod for Minecraft 1.16.1 [Add and generate trees]
[Java] Let's create a mod for Minecraft 1.14.4 [9. Add and generate trees]