/ TRAINING

구구단 2

실습

박재성님의 자바 플레이그라운드 - 2

0. 목차


5. 배열로 구구단 구현
6. 메서드로 구구단 구현
7. 클래스로 구구단 구현
8. 최종요구사항

1. 배열로 구구단 구현


public class Gugudan {
	public static void main(String[] arg) {
		int[] result = new int [9];
		for (int i = 0; i < result.length; i++) {
			result[i] = 2 * (i + 1);
			System.out.println(result[i]);
		}
	}
}

▷ int[] result = new int[9];

배열
9개의 칸이 있다고 생각하면 이와 같은 모양

예를 들어
result[1]은
1번 칸에 뭐가 있는지 묻는 것
1번 칸에는 0이 있음
[1]에는 1(X) 0(O)

▷ for (int i = 0; i < result.length; i++)

int i = 0; 배열은 0 부터 시작하기 때문
i < result.length; 배열의 길이는 0~8까지 총 9

▷ result[i] = 2 * (i + 1);

배열은 ‘0’ 부터 시작
그래서 ‘i + 1’

▷ System.out.println(result[i]);


//console
2
4
6
8
10
12
14
16
18

2. 메서드로 구구단 구현


▶ 9단을 메서드로 구현

public class Gugudan {
	
	public static int[] calculate(int times) {
		int[] result = new int[9];
		
		for(int i = 0; i < result.length; i++) {
			result[i] = times * (i + 1);
		}
		return result;
	}
	
	public static void main(String[] args) {
		int[] result = calculate(9);
		
		for(int i = 0; i < result.length; i++) {
			System.out.println(result[i]);
		}
	}
}

▷ Main 메서드 부터 실행

public static void main(String[] args) {
  int[] result = calculate(9);
  
  for(int i = 0; i < result.length; i++) {
  System.out.println(result[i]);
  } 
} 

int배열(정수배열)로 되어있는 result라는 사물함에는
9를 받는 calculate가 들어있음

▷ calculate

public static int[] calculate(int times) {
  int[] result = new int[9];
  
  for(int i = 0; i < result.length; i++) {
      result[i] = times * (i + 1);
  }
  return result;
}

calculate는 이렇게 생긴 메서드



int[] result = calculate(9);

public static int[] calculate(int times)

int times에 9를 넣어 출력하라는 것



calculate(9)를 호출 했고
calculate 메서드에서
return 값인 9단을 얻음

다시 main으로 가자

▷ 다시 main

public static void main(String[] args) {
    int[] result = calculate(9);
    
    for(int i = 0; i < result.length; i++) {
        System.out.println(result[i]);
    }
}

int[] result에는 calculate에서 얻은 return 값인 9단이 배열로 들어있음
이걸 반복문에 넣어서
차례로 꺼내는 것(println)


//console
9
18
27
36
45
54
63
72
81

▶ 5단을 메서드로 구현

public class Gugudan {
	
	public static int[] calculate(int times) {
		int[] result = new int[9];
		
		for(int i = 0; i < result.length; i++) {
			result[i] = times * (i + 1);
		}
		return result;
	}
	
	public static void main(String[] args) {
		int[] times5 = calculate(5);
		
		for(int i = 0; i < times5.length; i++) {
			System.out.println(times5[i]);
		}
	}
}




//console
5
10
15
20
25
30
35
40
45

▶ 메서드로 출력 구현 - 5단 출력

출력하는 메서드를 만들어서
for문 + System.out.println(); → 이 중복 없애기

public class Gugudan {
	
	public static int[] calculate(int times) {
		int[] result = new int[9];
		
		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) {
		int[] baek = calculate(5);
		print(baek);
	}
}

print라는 return 값이 없는 void 메서드를 생성

public static void print(int[] result) {
    for(int i = 0; i < result.length; i++) {
        System.out.println(result[i]);
    }
}

calculate 메서드의 return 값인 result를 받는 메서드
for문을 result의 배열길이만큼 돈 후
값을 출력

그래서 main 메서드 에서는
출력문을 생략하고 print 메서드를 호출

public static void main(String[] args) {
    int[] baek = calculate(5);
    print(baek);
}
//console
5
10
15
20
25
30
35
40
45

▶ 2단부터 9단까지 자동으로 출력되는 메서드 구현

▷ for문 사용


public class Gugudan {
	
	public static int[] calculate(int times) {
		int[] result = new int[9];
		
		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 < 10; i++) {
			int[] baek = calculate(i);
			print(baek);
		}
	}
}
//console
2
4
6
8
10
12
14
16
18
3
6
9
12
15
18
··· 9단까지 계속
  • main 메서드
    • for문 = i가 2~9까지 돌게 함
    • calculate(i); → i를 통해 2~9를 calculate 메서드에 뿌림
    • int[] baek = calculate(i) → 2단,3단,,, 차례로 baek 사물함에 보관
    • print 메서드를 통해 출력

3. 클래스로 구구단 구현


  • GugudanMain 클래스 생성
  • calculate 메서드는 Gugudan 클래스에 있기때문에
    Gugudan.calculate
  • print 메서드도 Gugudan 클래스에 있기때문에 Gugudan.print로 호출
public class GugudanMain {
	public static void main(String[] args) {
		for(int i = 2; i < 10; i++) {
			int[] baek = Gugudan.calculate(i);
			Gugudan.print(baek);
		}
	}
}
package baekGugudan;

import java.util.Scanner;

public class Gugudan {

	public static int[] calculate(int times) {
		int[] result = new int[9];

		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) {
		System.out.println("몇 단?");
		Scanner scanner = new Scanner(System.in);
		int number = scanner.nextInt();

		for (int j = 1; j < 10; j++) {
			System.out.println(number + "X" + j + "=" + number * j);
		}
		scanner.close();
	}
}
//console
몇 단?
5
5X1=5
5X2=10
5X3=15
5X4=20
5X5=25
5X6=30
5X7=35
5X8=40
5X9=45

4. 최종요구사항


최종 요구사항 1

  • 사용자가 입력한 값에 따라 크기가 다른 구구단을 계산해 출력한다.
  • 예를 들어 사용자가 8을 입력하면 팔팔단,
    19를 입력하면 십구십구단(2 * 1에서 19 * 19)을 계산해 출력한다.

▶ 혼자 해보기 - 힌트참고X

import java.util.Scanner;

public class Gugudan {
	public static void main(String[] args) {
		System.out.println("몇 단?");
		Scanner scanner = new Scanner(System.in);
		int number = scanner.nextInt();
		
		for(int i = 2; i <= number; i++) {
			for(int j = 1; j <= number; j++) {
				System.out.println(i + "X" + j + "=" + i * j);
			}
		}
	}
}
//console
몇 단?
5
2X1=2
2X2=4
2X3=6
2X4=8
2X5=10
3X1=3
3X2=6
3X3=9
3X4=12
3X5=15
4X1=4
4X2=8
4X3=12
4X4=16
4X5=20
5X1=5
5X2=10
5X3=15
5X4=20
5X5=25

최종 요구사항 2

  • 사용자가 입력한 값에 따라 크기가 다른 구구단을 계산해 출력한다.
  • 예를 들어 사용자가 “8,7”과 같은 문자열을 입력하면 팔칠단을 구현한다.
  • 팔칠단은 2 * 1 … 2 * 7, 3 * 1 … 3 * 7, … , 8 * 1 … 8 * 7 까지
    구현하는 것을 의미한다.

▶ 혼자 해보기 - 힌트참고X

import java.util.Scanner;

public class Gugudan {
	public static void main(String[] args) {
		System.out.println("2단부터 시작됩니다.");
		Scanner scanner = new Scanner(System.in);
		int i = scanner.nextInt();
		int j = scanner.nextInt();
		
		if(i < 2) {
			System.out.println("2보다 큰 수를 입력하세요.");
		} else {
			for(int a = 2; a <= i; a++) {
				for(int b = 1; b <= j; b++) {
					System.out.println(a + "X" + b + "=" + a * b);
				}
			}
		}
	}
}
//console
2단부터 시작됩니다.
8 7
2X1=2
2X2=4
2X3=6
2X4=8
2X5=10
2X6=12
2X7=14
3X1=3
3X2=6
3X3=9
3X4=12
3X5=15
3X6=18
3X7=21
4X1=4
4X2=8
4X3=12
4X4=16
4X5=20
4X6=24
4X7=28
5X1=5
5X2=10
5X3=15
5X4=20
5X5=25
5X6=30
5X7=35
6X1=6
6X2=12
6X3=18
6X4=24
6X5=30
6X6=36
6X7=42
7X1=7
7X2=14
7X3=21
7X4=28
7X5=35
7X6=42
7X7=49
8X1=8
8X2=16
8X3=24
8X4=32
8X5=40
8X6=48
8X7=56

▶ 힌트참고

import java.util.Scanner;

public class Gugudan {
	public static void main(String[] args) {
		
		System.out.println("입력하세요. " + "a,b");
		
		Scanner scanner = new Scanner(System.in);
		
		String inputValue = scanner.nextLine();
		
		String[] splitedValue = inputValue.split(",");
		
		int first = Integer.parseInt(splitedValue[0]);
		int second = Integer.parseInt(splitedValue[1]);
		System.out.println(first + "단까지 " + second + "만큼 곱하겠습니다!");
		
		for(int i = 2; i <= first; i++) {
			for (int j = 1; j <= second; j++) {
				System.out.println(i + "X" + j + "=" + i * j);
			}
		}
	}
}
//console
입력하세요. a,b
4,5
4단까지 5만큼 곱하겠습니다!
2X1=2
2X2=4
2X3=6
2X4=8
2X5=10
3X1=3
3X2=6
3X3=9
3X4=12
3X5=15
4X1=4
4X2=8
4X3=12
4X4=16
4X5=20

▶ 알게 된 점

  • String inputValue = scanner.nextLine();
    String 타입은 문자열 타입
    입력한 숫자 2개를 문자열로 받음

  • String[] splitedValue = inputValue.split(“,”);
    사용자가 입력한 숫자 2개를 []배열에 넣어서
    ,를 기준으로 분리

  • int first = Integer.parseInt(splitedValue[0]);
    int second = Integer.parseInt(splitedValue[1]);
    숫자 2개 중 첫째[0]는 first / 둘째[1]는 second에 넣어줌
    String → int
    정수타입으로 바꿔 줌