구구단 Assignment

Hansol Jeong·2022년 2월 2일

Java Playground

목록 보기
2/2

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));
		}
		
	}
	
}

0개의 댓글