View

[Beginner] 1303 : 숫자사각형1

다슬다슬 2021. 1. 17. 21:32

 

jungol.co.kr/bbs/board.php?bo_table=pbank&wr_id=2069&sca=2010

 

JUNGOL

 

www.jungol.co.kr

100 이하의 사각형의 높이 n과 너비 m을 입력받고, 너비를 1부터 차례대로 출력하되 m의 배수 단위로 끊어서 출력하면 된다.

 

 

while (n > 100 || m > 100) { // n or m이 100 초과라면 다시 입력
	System.out.println("INPUT ERROR!");
		n = sc.nextInt();
		m = sc.nextInt();
} 

n과 m이 100 이하의 정수이기 때문에 100을 초과하는 정수 입력 시 에러메시지를 표시하고 다시 입력받는다.

 

 

for(int i = 1; i <= n*m; i++) { // 1부터 n*m까지 증가하며 반복
	System.out.printf("%d ", i);
		if(i % m == 0) {
			System.out.println();
	}
}

그리고 1부터 n*m까지 1씩 증가하는 i를 출력하고,

만약 i를 입력했던 m으로 나눈 나머지가 0이면 한 줄 띄어주도록 했다.

 

 

n에 4, m에 5를 입력하면 위와 같은 결과를 볼 수 있다.

끗끗끗

 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import java.util.Scanner;
 
public class Main {
 
    public static void main(String[] args) {
 
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt(); // 사각형 높이 n
        int m = sc.nextInt(); // 사각형 너비 m
 
        while (n > 100 || m > 100) { // n or m이 100 초과라면 다시 입력
            System.out.println("INPUT ERROR!");
            n = sc.nextInt();
            m = sc.nextInt();
        }
 
        for (int i = 1; i <= n * m; i++) { // 1부터 n*m까지 증가하며 반복
            System.out.printf("%d ", i);
            if (i % m == 0) {
                System.out.println();
            }
        }
    }
}
 
cs

 

'Jungol' 카테고리의 다른 글

[Beginner] 1314 : 문자사각형2  (0) 2021.02.21
[Beginner] 2046 : 숫자사각형4  (0) 2021.01.26
[Beginner] 1341 : 구구단2  (0) 2021.01.15
[Beginner] 1291 : 구구단  (0) 2021.01.13
Share Link
reply
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31