1. Assignment 1
- 사용자가 입력한 값에 따라 크기가 다른 구구단을 계산해 출력한다.
- 예를 들어 사용자가 8을 입력하면 팔팔단, 19를 입력하면 십구십구단(2 * 1에서 19 * 19)을 계산해 출력한다.
- 이 요구사항은 앞의 실습을 소화했으면 구현할 수 있기 때문에 생략한다.
import java.util.Scanner;
public class TimesTableMission {
static Scanner scanner = new Scanner(System.in);
static int number = scanner.nextInt();
public static int[] calculate(int times) {
int[] result = new int[number];
for (int i=0; i < result.length; i++) {
result[i] = times * (i+1);
}
return result;
}
public static void print(int[] result) {
for (int i=0; i < result.length; i++) {
System.out.println(result[i]);
}
}
public static void main(String[] args) {
for (int i=2; i<=number; i++) {
print(calculate(i));
}
}
}
2. Assignment 2
- 사용자가 입력한 값에 따라 크기가 다른 구구단을 계산해 출력한다.
- 예를 들어 사용자가 "8,7"과 같은 문자열을 입력하면 팔칠단을 구현한다. 팔칠단은 2 * 1 ... 2 * 7, 3 * 1 ... 3 * 7, ... , 8 * 1 ... 8 * 7 까지 구현하는 것을 의미한다.
import java.util.Scanner;
public class TimesTableMission2 {
static Scanner scanner = new Scanner(System.in);
static String inputValue = scanner.nextLine();
static String[] splitedValue = inputValue.split(",");
static int first = Integer.parseInt(splitedValue[0]);
static int second = Integer.parseInt(splitedValue[1]);
public static int[] calculate(int times) {
int[] result = new int[second];
for (int i=0; i < result.length; i++) {
result[i] = times * (i+1);
}
return result;
}
public static void print(int[] result) {
for (int i=0; i < result.length; i++) {
System.out.println(result[i]);
}
}
public static void main(String[] args) {
for (int i=2; i<=first; i++) {
print(calculate(i));
}
}
}