blog.itcode.devblog.itcode.dev

[Programmers / JAVA] Level 1 Drawing a Rectangle with Stars (12954)

This problem gives you two integers n and m via standard input. Using the star (*) character, print out a rectangle with a width of n and a height of m.

[Programmers / JAVA] Level 1 Drawing a Rectangle with Stars (12954)

This problem gives you two integers n and m via standard input. Using the star (*) character, print out a rectangle with a width of n and a height of m.
RWB0104
@RWBwritten at 2021-12-19 07:18:31
Programmers

시리즈 모아보기

Programmers

64 / 78
RankLanguage Used
Level 1

🖼️ JAVA

🔗 Drawing a Rectangle with Stars

This problem gives you two integers n and m via standard input.

Using the star (*) character, print out a rectangle with a width of n and a height of m.

  • Each of n and m is a natural number no greater than 1000.

TXT

5 3

TXT

*****
*****
*****

This time, unusually, the input is taken directly from the user via a Scanner object. We take n and m directly and print a rectangle of that size filled with asterisks (*). This problem reminded me of when I first learned C in college.

Personally, I prefer using BufferedReader over Scanner for reading user input. The reason is that BufferedReader offers better performance.

Since such a subtle performance difference doesn't determine whether an algorithm passes or not, it's fine to just use whichever you prefer.


Enter n and m, and print n stars across m lines. Using the repeat() method, you can repeat a given string as many times as you want, allowing you to build this without a nested loop.

JAVA

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

/**
 * Drawing a Rectangle with Stars class
 *
 * @author RWB
 * @since 2021.12.13 Mon 22:27:04
 */
class Solution
{
	/**
	 * Main method
	 *
	 * @param args: [String[]] parameters
	 */
	public static void main(String[] args) throws IOException
	{
		BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
		
		int[] inputs = Arrays.stream(reader.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
		
		for (int i = 0; i < inputs[1]; i++)
		{
			System.out.println("*".repeat(inputs[0]));
		}
		
		reader.close();
	}
}
# Programmers# Algorithm# JAVA# Level 1
ship
blog.itcode.dev

Notes from the π-th Alpaca

7.0.1
Developed by RWB since 2021.057th upgraded at 2026.08