본문 바로가기

JAVA/알고리즘 예제

[JAVA] 5x5 사각형(1~25)


package javalesson.com.javalesson.ch01test;


public class T02RectBasic {

public static void main(String[] args) {

int array[][] = new int[5][5];

int row, col;

int i=1;

for(row=0;row<5;row++){

for(col=0;col<5;col++){

array[row][col]=i++;

}

}

                //출력

for(row=0;row<5;row++){

for(col=0;col<5;col++){

System.out.printf("%3d",array[row][col]); //글자수를 3개로 배열을 출력

}

System.out.println();

}

}

}



package javalesson.com.javalesson.ch01test;


public class T02RectBasic {

public static void main(String[] args) {

                Scanner scan = new Scanner(System.in);

                int deg;

while(true){

try{

System.out.print("사각형의 차수를 입력하세요. : ");

deg = scan.nextInt();

break;

}catch(InputMismatchException e){

System.out.println("숫자만 입력해야 합니다.");

}

}

scan.close();

int array[][] = new int[deg][deg];

int row, col;

int i=1;

for(row=0;row<deg;row++){

for(col=0;col<deg;col++){

array[row][col]=i++;

}

}

                //출력

for(row=0;row<deg;row++){

for(col=0;col<deg;col++){

System.out.printf("%3d",array[row][col]); //글자수를 3개로 배열을 출력

}

System.out.println();

}

}

}


'JAVA > 알고리즘 예제' 카테고리의 다른 글

[JAVA] 선택 정렬(Selection Sort)  (1) 2016.08.31
[JAVA] 마방진  (1) 2016.08.31
[JAVA] 모래시계 찍기  (1) 2016.08.30
[JAVA] 다이아몬드 찍기  (1) 2016.08.30
[JAVA] 달팽이 로직  (2) 2016.08.29