슬슬 간단한 구구단 프로그래밍 로직을 짜고, 실행시키고 github에 올리는 것이 익숙해지고 있다.
한 2~3시간 연속 공부하고 나니 지치고, 하기 싫어진다.
1시간 밍기적거리고 다시 30분 앉아서 얼레벌레 오늘의 목표까지 공부했지만...
이대로는 죽도 밥도 안 된다는 생각이 든다.
...어차피 지금 일을 그만둔다고 해도 일일 최대 시간 효율 못 뽑았네...
지금도 이렇게 2~3시간만에 쭉 뻗어버리는 판국에...
좀 더 습관화를 들여, 공부 집중 지구력을 키울 필요가 있는 것 같다.
어쨌든 이래저래, 배열과 별도의 메소드, 별도의 클래스 다루는 것까지 배웠네...
int[]
이고, new int[x]
로 크기 x만큼의 새로운 배열을 해당 변수에 확보할 수 있다.// ※ 클래스 및 메인 메소드 생략
//크기가 9인 새로운 정수형 배열을 변수명 second에 선언
int[] second = new int[9];
배열의 크기는 배열변수명.length
라는 정수형 변수로 지정할 수 있다.
배열의 i번째 위치에 해당하는 자료는 배열변수명[i]
로 지정하여 다룰 수 있다.
// ※ 클래스 및 메인 메소드 생략
for (int i=0; i < second.length; i++) { //배열 second의 각 위치 i(0~8)에 대해여 반복문 실행
second[i] = 2 * (i + 1); //배열 second의 i번째 위치에 2단 곱셈 결과값 대입
System.out.println(second[i]); //배열 second의 i번째 위치의 결과값 출력
} //반복문 종료
//위와 같은 방법으로 구구단 2단의 결과값 9가지를 배열 형태로 하나의 변수에 저장할 수 있다.
// ※ 클래스 및 메인 메소드 생략
System.out.println("2~9단까지 2중 for문으로 구현");
int[] multiply_all = new int[9]; //구구단 한 단의 결과값의 배열을 변수 multiply_all에 할당
for (int number=2; number < 10; number++) { //2단부터 9단까지 number단 단위 반복문 시작
System.out.println(number + "단");
for (int i=0; i < multiply_all.length; i++) { //한 단 내에서 1~9까지 곱셈 반복문 시작
multiply_all[i] = number * (i + 1); //곱셈 연산 결과값을 순서에 맞게 저장
System.out.println(multiply_all[i]); //저장된 결과값 출력
} //단 내 곱셈 반복문 종료
System.out.println("");
} //number단 단위 반복문 종료
public static 반환값 메소드명(입력값)
이다.public static int[] calculate(int times)
: times라는 정수값을 calculate라는 로직에 받아서 int[] 형태의 결과값으로 반환시키는 메소드를 선언한다.
public static void main(String[] args)
: 결과값을 반환하지 않는 main메소드를 선언하여 실행시킨다. (void는 결과값을 반환하지 않음을 의미한다.)
- System.out.println()으로 콘솔 화면에 출력된 값은 보여주기일 뿐, 사용할 수 있는 형태로 저장, 반환을 하지는 않는다.
public class GugudanWithMethods {
//입력한 단(times)을 구구단 곱셈 결과의 배열(result)로 반환시키는 메소드(calculate) 선언
public static int[] calculate(int times) { //times 정수 받고 정수 배열 반환하는 메소드 시작
int[] result = new int[9]; //새로운 정수 배열 result 생성
for (int i=0;i < result.length; i++) { //곱셈 결과값으로 정수 배열 result를 채우는 반복문 실행
result[i] = times * (i + 1); //받은 times 정수로 곱셈 결과값 대입
} //반복문 종료
return result; //완성된 정수 배열 result 반환
} //calculate 메소드 종료
//위에서 선언한 calculate 메소드를 실행시킨 결과값을 출력시키는 main 메소드 선언 및 실행
public static void main(String[] args) { //값을 반환하지 않는 main 메소드 시작
int[] result = calculate(2); //위에서 선언한 calculate 메소드에 2(단수)를 넣은 결과값 배열 호출
for (int i=0; i<result.length; i++) { //호출한 배열 안의 값을 출력하는 반복문 실행
System.out.println(result[i]); //i번째 값 호출
} //반복문 종료
} //main 메소드 종료
public class GugudanWithMethods {
//입력한 단(times)을 구구단 곱셈 결과의 배열(result)로 반환시키는 메소드(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 메소드를 실행시킨 결과값을 출력시키는 메소드(print) 선언
public static void print(int[] result) {
for (int i=0;i < result.length; i++) {
System.out.println(result[i]);
}
}
//calculate 메소드로 계산, print 메소드로 출력시키는 main메소드 선언 및 실행
public static void main(String[] args) {
for (int number=2; number<10; number++) {
System.out.println(number + "단");
int[] result = calculate(number);
print(result);
System.out.println("");
}
}
}
public static 반환값 클래스명.메소드명(입력값)
이다.public class GugudanWithMethods {
//입력한 단(times)을 구구단 곱셈 결과의 배열(result)로 반환시키는 메소드(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 메소드를 실행시킨 결과값을 출력시키는 메소드(print) 선언
public static void print(int[] result) {
for (int i=0;i < result.length; i++) {
System.out.println(result[i]);
}
}
}
public class GugudanMain {
//외부 클래스(GugudanWithMethods)에 있는 메소드를 실행
public static void main(String[] args) {
for (int number=2; number<10; number++) {
System.out.println(number + "단");
int[] result = GugudanWithMethods.calculate(number); //GugudanWithMethods 클래스의 calculate 메소드로 구구단 계산
GugudanWithMethods.print(result); //GugudanWithMethods 클래서의 print 메소드로 결과 출력
System.out.println("");
}
}
}