package Day04;
public class Q1 {
static void main() {
int[][] arr = new int[5][5];
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length; j++) {
if (i % 2 == 0) {
arr[i][j] = i * 5 + 1 +j;
} else {
arr[i][j] = i * 5 + 1 + (4 - j);
}
}
}
}
}
배열은 아무리해도 아직은 모르겠어서 시간안에 풀지 못했다.
package Day04;
public class Q1_1 {
static void main() {
// 고정하는 변동하는 부분
// 행과 열 반복 변동을 하니까 이거만 고민하면 되는데
// 값을 넣는 부분 변동
int[][] arr = new int[5][5];
int num = 1;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
arr[i][j] = num++;
}
}
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
System.out.print(arr[i][j] + "\t");
}
System.out.println();
}
System.out.println("==================");
num = 1;
for (int i = 0; i < arr.length; i++) {
// 1. 짝수인 경우는 정방향 0 -> 4
// 2. 홀수인 경우는 역방향 4 -> 0
if (i % 2 == 0) {
for (int j = 0; j < arr[i].length; j++) {
arr[i][j] = num++;
}
} else {
for (int j = arr[i].length -1; j >= 0; j--) {
arr[i][j] = num++;
}
}
}
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
System.out.print(arr[i][j] + "\t");
}
System.out.println();
}
}
}
package Day04;
import java.util.Scanner;
public class Q2 {
static void main() {
Scanner sc = new Scanner(System.in);
System.out.print("숫자 입력 : ");
int User = sc.nextInt();
int[][] arr = new int[User][User];
//규칙
// 열 행 열 행 열 행 열 행 열
// 5 -> 4 -> 4 -> 3 -> 3-> 2-> 2-> 1 -> 1
// ++ ++ -- -- ++ ++ -- -- ++
int K = User;
int C = -1; // 열
int R = 0; // 행
int N = 1; // 값
int SW = 1; // 증감 제어 변수
while (true){
//열
for(int i = 0;i<K;i++){
C += SW;
// 0 0 1
// 0 1 2
// 0 2 3
// 0 3 4
// 0 4 5
// 4 3 10
// 4 2 11
// 4 1 12
// 4 0 13
arr[R][C] = N++;
}
K--;
if(K == 0){
break;
}
//행
for(int i = 0;i<K;i++){
R += SW;
// 1 4 6
// 2 4 7
// 3 4 8
// 4 4 9
arr[R][C] = N++;
}
SW *= -1;
}
for(int i = 0;i<arr.length;i++){
for(int j = 0;j<arr[i].length;j++){
System.out.print(arr[i][j]+"\t");
}
System.out.println();
}
}
}
package Day04;
public class MultiArrayTest {
static void main() {
int[][][] arr = new int[2][][];
arr[0] = new int[3][];
arr[1] = new int[4][];
arr[0][0] = new int[7];
arr[1][3] = new int[3];
arr[0] = new int[3][2];
// arr[0][0][0~6] int
// arr[1][3][0~2] int
}
}
package Day04;
public class MethodTest {
// 1.
static String printHello(String name) {
return "안녕하세요" + name + "님";
}
// 2.
static int sum(){
return 10 + 20;
}
// 3.
static void outputName(String name) {
System.out.println("안녕하세요" + name + "님");
}
// 4.
static void printBye() {
System.out.println("바이!!!!!!");
}
static void main() {
String result=printHello("홍길동");
System.out.println(result);
int r = sum();
outputName("홍길동");
printBye();
}
}
package Day04;
import java.util.Scanner;
public class Q3 {
static void main() {
Scanner sc = new Scanner(System.in);
System.out.print("연산자 입력: ");
String user1 = sc.next();
if (!(user1.equals("+") || user1.equals("-") || user1.equals("*") || user1.equals("/"))) {
System.out.println("잘못된 연산자입니다.");
} else {
System.out.print("숫자 1 입력: ");
int user2 = sc.nextInt();
System.out.print("숫자 2 입력: ");
int user3 = sc.nextInt();
int num = 0;
if (user1.equals("+")) {
num = user2 + user3;
} else if (user1.equals("-")) {
num = user2 - user3;
} else if (user1.equals("*")) {
num = user2 * user3;
} else {
num = user2 / user3;
}
System.out.print("결과 값: " + num);
}
}
}
이제 이런 코드는 시간이 좀 걸리지만 혼자서 풀 수 있게 되었다.
아직 시간이 부족해서 메서드는 만들지 못하였지만 메인 안에서 출력은 되게 만들었다.
package Day04;
import java.util.Scanner;
public class Q3_1 {
static int x, y;
// 1. 더하기 연산(주고 받고)
static int add(int a, int b) {
return a + b;
}
// 2. 빼기 연산(주고 안받고)
static int minus() {
return x - y;
}
// 3. 곱하기 연산(안주고 받고)
static void mul(int a, int b) {
System.out.println("결과 값: " + (a * b) + "입니다.");
}
// 4. 나누기 연산(안주고 안받고)
static void div() {
// System.out.println("결과 값: " + ((double)x / y) + "입니다.");
System.out.printf("결과 값: %.1f 입니다.", (double)x / y);
}
static void main() {
// 입력
Scanner sc = new Scanner(System.in);
System.out.print("연산자 입력: ");
String op = sc.next();
System.out.print("숫자 1 입력: ");
int num1 = sc.nextInt();
System.out.print("숫자 2 입력: ");
int num2 = sc.nextInt();
switch (op) {
case "+" :
// int result = add(num1, num2);
// System.out.println("결과 값: " + result + "입니다.");
System.out.println("결과 값: " + add(num1, num2) + "입니다.");
break;
case "-" :
x = num1;
y = num2;
int result = minus();
System.out.println("결과 값: " + result + "입니다.");
break;
case "*" :
mul(num1, num2);
break;
case "/" :
x = num1;
y = num2;
div();
break;
default:
System.out.println("잘못된 연산자입니다.");
break;
}
}
}
package Day04;
import java.util.Scanner;
public class Q3_2 {
// 1. 더하기 연산(주고 받고)
static int add(int a, int b) {
return a + b;
}
// 2. 빼기 연산(주고 안받고)
static int minus() {
// 숫자 입력
Scanner sc = new Scanner(System.in);
System.out.println("숫자 1 입력 : ");
int x = sc.nextInt();
System.out.println("숫자 2 입력 : ");
int y = sc.nextInt();
return x - y;
}
// 3. 곱하기 연산(안주고 받고)
static void mul(int a, int b) {
System.out.println("결과 값: " + (a * b) + "입니다.");
}
// 4. 나누기 연산(안주고 안받고)
static void div() {
// System.out.println("결과 값: " + ((double)x / y) + "입니다.");
Scanner sc = new Scanner(System.in);
System.out.println("숫자 1 입력 : ");
int x = sc.nextInt();
System.out.println("숫자 2 입력 : ");
int y = sc.nextInt();
System.out.printf("결과 값 : %.1f 입니다", (double)x / y);
}
static void main() {
// 입력
Scanner sc = new Scanner(System.in);
System.out.print("연산자 입력: ");
String op = sc.next();
if(op.equals("-")) {
int result = minus();
System.out.println("결과 값: " + result + "입니다.");
} else if (op.equals("/")) {
div();
} else {
System.out.print("숫자 1 입력: ");
int num1 = sc.nextInt();
System.out.print("숫자 2 입력: ");
int num2 = sc.nextInt();
switch (op) {
case "+" :
// int result = add(num1, num2);
// System.out.println("결과 값: " + result + "입니다.");
System.out.println("결과 값: " + add(num1, num2) + "입니다.");
break;
case "*" :
mul(num1, num2);
break;
default:
System.out.println("잘못된 연산자입니다.");
break;
}
}
}
}
두 코드 모두 계산기를 구현한 예제이지만, 입력을 받는 위치와 메서드의 역할이 다르다.
main()에서 한 번만 받는다.main() → 입력 → 메서드 호출 → 결과 출력
minus(), div())에서 직접 Scanner를 사용해 입력을 받는다.main() → 메서드 호출 → 메서드에서 입력 → 계산 → 결과 출력
실제 프로젝트에서는 Q3_1처럼 입력은 main()에서 받고, 메서드는 계산만 담당하도록 작성하는 방식이 더 많이 사용된다.
package Day04;
public class MethodTest2 {
static void a() {
b();
k();
System.out.println("A");
}
static void b() {
c();
k();
System.out.println("B");
}
static void c() {
d();
System.out.println("C");
}
static void d() {
e();
System.out.println("D");
}
static void e() {
System.out.println("E");
}
static void k() {
System.out.println("A");
}
static void main() {
a();
}
}
package Day04;
import java.util.Scanner;
public class Q4 {
static int rec(int leng, int wid) {
return leng * wid;
}
static void tri(int leng, int wid) {
System.out.println(leng * wid / 2);
}
static void main() {
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("1. 사각형 넓이 2. 삼각형 넓이 3. 종료");
int user = sc.nextInt();
if (user == 3) {
System.out.println("프로그램을 종료합니다.");
break;
} else if (user != 1 && user != 2) {
System.out.println("잘못된 입력입니다.");
continue;
}
System.out.print("가로: ");
int leng = sc.nextInt();
System.out.print("세로: ");
int wid = sc.nextInt();
if (user == 1) {
System.out.println(rec(leng, wid));
} else {
tri(leng, wid);
}
}
}
}
이번엔 메서드까지 구현은 성공하였지만 시간이 부족해 입력 메서드는 구현하지 못했다.
시간이 조금 더 있었으면 가능했었을지도 모르겠다.
package Day04;
import java.util.Scanner;
public class Q4_1 {
// 1. 사각형 넓이 구하는 함수 (주고 받고)
static int rectangle(int w, int h) {
return w * h;
}
// 2. 삼각형 넓이 구하는 함수 (안주고 받고)
static void triangle(int w, int h) {
System.out.println("삼각형의 넓이: " + w * h / 2);
}
// 3. 입력 함수 (주고 안받고)
static int[] input() {
Scanner sc = new Scanner(System.in);
int[] in = new int[2];
System.out.print("가로: ");
in[0] = sc.nextInt();
System.out.print("세로: ");
in[1] = sc.nextInt();
return in;
}
static void main() {
// 1. 3 입력 전까지는 계속 반복
// 2. 입력 -> 정수 입력
// 1. 사각형 넓이 2. 삼각형 넓이 3. 종료
// 1, 2를 입력시 사각형 넓이 구하거나 삼각형 넓이를 구한다.
// 1~3 이외의 값을 입력하면 잘못된 입력입니다. 출력
Scanner sc = new Scanner(System.in);
int w, h;
int[] temp;
boolean check = true;
while (check) {
System.out.println("1. 사각형 넓이 2. 삼각형 넓이 3. 종료");
int num = sc.nextInt();
switch (num) {
case 1: // 사각형
temp = input();
System.out.println("사각형의 넓이: " + rectangle(temp[0], temp[1]));
break;
case 2: // 삼각형
temp = input();
triangle(temp[0], temp[1]);
break;
case 3:
check = false;
System.out.print("프로그램을 종료합니다.");
break;
default:
System.out.println("잘못된 입력입니다.");
break;
}
}
}
}
package Day04;
// 변수 없는 ver
import java.util.Scanner;
public class Q4_2 {
// 1. 사각형 넓이 구하는 함수 (주고 받고)
static int rectangle(int[] temp) {
return temp[0] * temp[1];
}
// 2. 삼각형 넓이 구하는 함수 (안주고 받고)
static void triangle(int[] temp) {
System.out.println("삼각형의 넓이: " + temp[0] * temp[1]/2);
}
// 3. 입력 함수 (주고 안받고)
static int[] input() {
Scanner sc = new Scanner(System.in);
int[] in = new int[2];
System.out.print("가로: ");
in[0] = sc.nextInt();
System.out.print("세로: ");
in[1] = sc.nextInt();
return in;
}
static void main() {
// 1. 3 입력 전까지는 계속 반복
// 2. 입력 -> 정수 입력
// 1. 사각형 넓이 2. 삼각형 넓이 3. 종료
// 1, 2를 입력시 사각형 넓이 구하거나 삼각형 넓이를 구한다.
// 1~3 이외의 값을 입력하면 잘못된 입력입니다. 출력
Scanner sc = new Scanner(System.in);
int w, h;
int[] temp;
boolean check = true;
while (check) {
System.out.println("1. 사각형 넓이 2. 삼각형 넓이 3. 종료");
int num = sc.nextInt();
switch (num) {
case 1: // 사각형
System.out.println("사각형의 넓이: " + rectangle(input()));
break;
case 2: // 삼각형
triangle(input());
break;
case 3:
check = false;
System.out.print("프로그램을 종료합니다.");
break;
default:
System.out.println("잘못된 입력입니다.");
break;
}
}
}
}
두 코드는 사각형과 삼각형의 넓이를 구하는 동일한 기능을 가지고 있지만, 데이터 처리 방식과 메서드 구조가 다르다.
package Practice.Day1.Day2;
// 1부터 10까지 배열을 사용해서 출력하기
public class Q1 {
static void main() {
int[] arr = new int[10];
for (int i = 0; i < 10; i++) {
arr[i] = i + 1;
}
for (int i = 0; i < 10; i++) {
System.out.print(arr[i] + " ");
}
}
}
package Practice.Day1.Day2;
// 배열을 사용하여 1부터 10까지 역순으로 출력하라.
public class Q2 {
static void main() {
int[] arr = new int[10];
for (int i = 0; i < 10; i++) {
arr[i] = i + 1;
}
for (int i = arr.length -1; i >= 0; i--) {
System.out.print(arr[i] + " ");
}
}
}
package Practice.Day1.Day2;
// 배열을 사용해 2의 배수가 출력되도록 하라.
public class Q3 {
static void main() {
int[] arr = new int[10];
for (int i = 0; i < 10; i++) {
arr[i] = (i + 1) * 2;
}
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}
package Practice.Day1.Day2;
// 등차수열 배열 만들어서 출력하기
public class Q4 {
static void main() {
int[] arr = new int[10];
for (int i = 0; i < 10; i++) {
arr[i] = 1 + (i * 3);
}
for (int i = 0; i < 10; i++) {
System.out.print(arr[i] + " ");
}
}
}
package Practice.Day1.Day2;
// 등차수열을 배열에 저장한 뒤 역순으로 출력하기
public class Q5 {
static void main() {
int[] arr = new int[10];
for (int i = 0; i < 10; i++) {
arr[i] = 1 + (i * 3);
}
for (int i = 9; i >= 0; i--) {
System.out.print(arr[i] + " ");
}
}
}
package Practice.Day1.Day2;
// 5의 배수 배열 출력하기
public class Q6 {
static void main() {
int[] arr = new int[10];
for (int i = 0; i < arr.length; i++) {
arr[i] = (1 + i) * 5;
}
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}
package Practice.Day1.Day2;
// 5의 배수 역순으로 출력
public class Q7 {
static void main() {
int[] arr = new int[10];
for (int i = 0; i < arr.length; i++) {
arr[i] = (i + 1) * 5;
}
for (int i = 9; i >= 0; i--) {
System.out.print(arr[i] + " ");
}
}
}
package Practice.Day1.Day2;
// 1부터 10까지 출력하되 7일경우에만 HIT! 출력
public class Q8 {
static void main() {
for (int i = 1; i <= 10; i++) {
if (i == 7) {
System.out.print("HIT! ");
} else {
System.out.print(i + " ");
}
}
}
}
package Practice.Day1.Day2;
/*
요구사항
초기 HP는 10
1부터 10까지 for문 반복
규칙:
i가 3이면 HP -3
i가 7이면 HP +5
나머지는 HP 변화 없음
최종 HP 출력
*/
public class Q9 {
static void main() {
int hp = 10;
for (int i = 1; i <= 10; i++) {
if (i == 3) {
hp = hp - 3;
}
if (i == 7) {
hp = hp + 5;
}
}
System.out.println("최종 HP: " + hp);
}
}
아직 for문과 배열에 있어서 많이 약한 모습을 보이고 있다.
그래서 for문과 배열 문제가 나오면 시간을 오래 잡아먹기도 하고 가끔은 선생님 설명을 놓칠 때도 있다.
그래도 꾸준히 문제를 풀면서 전보다 나아진 나를 보니 뿌듯하다.
앞으로 많은 문제들을 풀어서 빨리 혼자서 시간안에 문제를 풀 수 있게 되었으면 좋겠다.