728x90
반응형
package practice.demensionArray;
import java.util.Scanner;
public class DemensionArrayPractice {
public void method1() {
//가변 배열 초기화 및 출력
Scanner sc = new Scanner(System.in);
System.out.print("행의 크기 입력 : ");
char[][] arr = new char[sc.nextInt()][];
//열 할당
for(int i=0; i<arr.length; i++) {
System.out.print(i + "행의 열의 크기 입력 : ");
arr[i] = new char[sc.nextInt()];
}
//초기화와 출력
char ch = 'a';
for(int i=0; i<arr.length; i++) {
for(int j=0; j<arr[i].length; j++) {
arr[i][j] = ch++;
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
}
💫 나의 풀이
package prac1.exam;
import java.util.Arrays;
import java.util.Scanner;
public class Blehye {
public void practice() {
Scanner sc = new Scanner(System.in);
System.out.print("행의 크기 입력 : ");
int rowSize = sc.nextInt(); //행의 크기
char a[][] = new char[rowSize][];
char ch = 'a';
for(int i=0; i<a.length; i++) {
System.out.print(i + "행의 열의 크기 입력 : ");
int colSize = sc.nextInt();
a[i] = new char [colSize];
}
for(int i=0; i<a.length; i++ ) {
for(int j=0; j<a[i].length; j++) {
a[i][j] = ch;
System.out.print(a[i][j]+ " ");
ch++;
}
System.out.println();
}
}
}
package practice.demensionArray;
import java.util.Scanner;
public class DemensionArrayPractice {
public void method2() {
Scanner sc = new Scanner(System.in);
char[][] arr = new char[5][5];
int row;
int column;
while(true) {
System.out.print("행 인덱스 입력 >> ");
row = sc.nextInt();
System.out.print("열 인덱스 입력 >> ");
column = sc.nextInt();
if(row<0 || row>4 || column<0 || column>4) {
break;
}
arr[row][column] = 'X';
System.out.println("\t0\t1\t2\t3\t4");
for(int i=0; i<arr.length; i++) {
System.out.print(i+ "\t");
for(int j=0; j<arr[i].length; j++) {
System.out.print(arr[i][j] + "\t");
}
System.out.println();
}
}
System.out.println("프로그램을 종료합니다.");
}
}
💫 2번 문제에서 처음 행,열 값도 먼저 입력 받도록 풀이
package prac1.exam;
import java.util.Arrays;
import java.util.Scanner;
public class Blehye {
public void practice() {
Scanner sc = new Scanner(System.in);
System.out.print("행의 크기 입력 : ");
int rowSize = sc.nextInt();
System.out.print("열의 크기 입력 : ");
int colSize = sc.nextInt();
char a[][] = new char[rowSize+1][colSize+1];
for(int i=1; i<=colSize ; i++) {
a[0][i] = (char)(i-1 + '0');
}
for(int i=1; i<=rowSize ; i++) {
a[i][0] = (char)(i-1 + '0');
}
while(true) {
System.out.print("행 인덱스 입력 >> ");
int rowNum = sc.nextInt();
System.out.print("열 인덱스 입력 >> ");
int colNum = sc.nextInt();
if(rowNum<0 || rowNum>=rowSize || colNum<0 || colNum>=colSize) {
break;
}
a[rowNum+1][colNum+1] = 'X';
for(int i=0 ; i<a.length ; i++) {
for(int j=0; j<a[i].length ; j++) {
System.out.print(a[i][j]+"\t");
}
System.out.println();
}
}
}
}
💡 2차원 배열 a의 자료형이 char이므로 가장자리에 숫자를 넣을 때 int를 char로 변환해주어야 했다.
=> (char)(int형 값 + '0') 으로 하면 아스키코드 문자로 나오지 않고 숫자 그대로 나온다.
728x90
반응형
'Dev > Java' 카테고리의 다른 글
[객체(1)] 객체 지향 언어, 절차 지향 언어, 클래스, 추상화, 캡슐화, 객체 (0) | 2022.05.10 |
---|---|
[JAVA기초(17)] 빙고 게임 실습 (0) | 2022.05.09 |
[JAVA기초(15)] 2차원 배열 (0) | 2022.05.08 |
[JAVA기초(14)] 배열 실습(1), (2) (0) | 2022.05.08 |
[JAVA기초(13)] 배열 Array, 배열 복사 (0) | 2022.05.08 |