package com.codestates;
import java.util.Scanner;
public class GugudanMain {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String inputValue = scan.nextLine();
String[] splitedValue = inputValue.split(",");
int first = Integer.parseInt(splitedValue[0]);
int second = Integer.parseInt(splitedValue[1]);
System.out.println(first + "," + second + "단을 출력합니다");
for (int j = 2; j <= first; j++) {
int[] result = Gugudan.calculate(j, second);
Gugudan.print(result);
}
}
}
package com.codestates;
public class Gugudan {
public static void print(int[] result) {
for(int i = 0; i < result.length; i++) {
System.out.println((result[i]));
}
}
public static int[] calculate(int times, int second) {
int[] result = new int[second];
for(int i = 0; i < result.length; i++) {
for(int j = 0; j < second; j++) {
result[j] = times * (j+1);
}
}
return result;
}
}
규칙:
사용자가 "8,7"을 입력하면 2*1, 2*2, ... 2*7, ...8*1, ... 8*7을 출력한다.
- 사용자 입력 받는 법
Scanner scan = new Scanner(System.in); String inputValue = scan.nextLine();
public static int[]
: public하게 접근 가능, static은 특정 메소드를 바로 사용하기 위한 선언자, int[]는 리턴할 값의 자료형- GugudanMain에서는 Gugudan class에 있는 calculate와 print 메소드를 이용해 실행한다
- main안에 있어야 실행 가능
int [] result = new int[숫자]
형식으로 배열 선언이 가능. 배열의 길이는 javascript와 다르게 length로 변경할 수 없다.- result 배열에 각 단의 계산 결과를 저장 후 main에서 출력한다
'Java' 카테고리의 다른 글
Java class 쪼개기 (0) | 2022.02.08 |
---|