본문 바로가기
프로그래밍/자바의정석_4_연습문제

[4-12] 구구단의 일부분을 다음과 같이 출력하시오.

by 므시칸곰틔군 2014. 12. 22.

public class Ex4_12 {

// [4-12] 구구단의 일부분을 다음과 같이 출력하시오.

//

// [실행결과]

// 2*1=2 3*1=3 4*1=4

// 2*2=4 3*2=6 4*2=8

// 2*3=6 3*3=9 4*3=12

//

// 5*1=5 6*1=6 7*1=7

// 5*2=10 6*2=12 7*2=14

// 5*3=15 6*3=18 7*3=21

//

// 8*1=8 9*1=9

// 8*2=16 9*2=18

// 8*3=24 9*3=27


public static void main(String[] args) {


a();

System.out.println();

System.out.println();

System.out.println();

b();


}


public static void a() {

for (int i = 1; i < 4; i++) {

for (int j = 1; j < 10; j++) {


// System.out.print(j + "x" + i + "=" + (j * i) + "\t");


int x = j + 1 + (i - 1) / 3 * 3;

int y = i % 3 == 0 ? 3 : i % 3;

if (x > 9) // 9단까지만 출력한다. 이 코드가 없으면 10단까지 출력된다.

break;

System.out.print(x + "*" + y + "=" + x * y + "\t"); //

// println이아님에 주의

}

System.out.println();

if (i % 3 == 0)

System.out.println(); //

}


}


public static void b() {

for (int i = 1; i < 4; i++) {

for (int j = 2; j < 10; j++) {


System.out.print(j + "x" + i + "=" + (j * i) + "\t");


}

System.out.println();

}


}

}