반복문을 활용한 구구단

Dec 19, 2023
반복문을 활용한 구구단
 
public static void main(String[] args) { System.out.println("2*1=2"); System.out.println("2*2=4"); System.out.println("2*3=6"); System.out.println("2*4=8"); System.out.println("2*5=10"); System.out.println("2*6=12"); System.out.println("2*7=14"); System.out.println("2*8=16"); System.out.println("2*9=18"); System.out.println(); System.out.println("3*1=3"); System.out.println("3*2=6"); System.out.println("3*3=9"); System.out.println("3*4=12"); System.out.println("3*5=15"); System.out.println("3*6=18"); System.out.println("3*7=21"); System.out.println("3*8=24"); System.out.println("3*9=27"); System.out.println(); System.out.println("4*1=4"); System.out.println("4*2=8"); System.out.println("4*3=12"); System.out.println("4*4=16"); System.out.println("4*5=20"); System.out.println("4*6=24"); System.out.println("4*7=28"); System.out.println("4*8=32"); System.out.println("4*9=36"); System.out.println(); System.out.println("5*1=5"); System.out.println("5*2=10"); System.out.println("5*3=15"); System.out.println("5*4=20"); System.out.println("5*5=25"); System.out.println("5*6=30"); System.out.println("5*7=35"); System.out.println("5*8=40"); System.out.println("5*9=45"); System.out.println(); System.out.println("6*1=6"); System.out.println("6*2=12"); System.out.println("6*3=18"); System.out.println("6*4=24"); System.out.println("6*5=30"); System.out.println("6*6=36"); System.out.println("6*7=42"); System.out.println("6*8=48"); System.out.println("6*9=54"); System.out.println(); System.out.println("7*1=7"); System.out.println("7*2=14"); System.out.println("7*3=21"); System.out.println("7*4=24"); System.out.println("7*5=35"); System.out.println("7*6=42"); System.out.println("7*7=48"); System.out.println("7*8=56"); System.out.println("7*9=63"); System.out.println(); System.out.println("8*1=8"); System.out.println("8*2=16"); System.out.println("8*3=24"); System.out.println("8*4=32"); System.out.println("8*5=40"); System.out.println("8*6=48"); System.out.println("8*7=56"); System.out.println("8*8=64"); System.out.println("8*9=72"); System.out.println(); System.out.println("9*1=9"); System.out.println("9*2=18"); System.out.println("9*3=27"); System.out.println("9*4=36"); System.out.println("9*5=45"); System.out.println("9*6=54"); System.out.println("9*7=63"); System.out.println("9*8=72"); System.out.println("9*9=81"); System.out.println(); }
 
방법은 모르겠는데 일단 나열했다.
 
공통된게 보인다. 2단 3단 4단…..을 변수로 바꾸면 복붙이 쉬울 것 같다.
 
int x; x = 2; System.out.println(x + "*1=" + (x * 1)); System.out.println(x + "*2=" + (x * 2)); System.out.println(x + "*3=" + (x * 3)); System.out.println(x + "*4=" + (x * 4)); System.out.println(x + "*5=" + (x * 5)); System.out.println(x + "*6=" + (x * 6)); System.out.println(x + "*7=" + (x * 7)); System.out.println(x + "*8=" + (x * 8)); System.out.println(x + "*9=" + (x * 9)); System.out.println();
 
x를 변수로 식을 만들었다. 이러면 x 값만 바꾸면 쉽게 복붙할 수 있다.
 
public static void main(String[] args) { int x; x = 2; System.out.println(x + "*1=" + (x * 1)); System.out.println(x + "*2=" + (x * 2)); System.out.println(x + "*3=" + (x * 3)); System.out.println(x + "*4=" + (x * 4)); System.out.println(x + "*5=" + (x * 5)); System.out.println(x + "*6=" + (x * 6)); System.out.println(x + "*7=" + (x * 7)); System.out.println(x + "*8=" + (x * 8)); System.out.println(x + "*9=" + (x * 9)); System.out.println(); x = 3; System.out.println(x + "*1=" + (x * 1)); System.out.println(x + "*2=" + (x * 2)); System.out.println(x + "*3=" + (x * 3)); System.out.println(x + "*4=" + (x * 4)); System.out.println(x + "*5=" + (x * 5)); System.out.println(x + "*6=" + (x * 6)); System.out.println(x + "*7=" + (x * 7)); System.out.println(x + "*8=" + (x * 8)); System.out.println(x + "*9=" + (x * 9)); System.out.println(); x = 4; System.out.println(x + "*1=" + (x * 1)); System.out.println(x + "*2=" + (x * 2)); System.out.println(x + "*3=" + (x * 3)); System.out.println(x + "*4=" + (x * 4)); System.out.println(x + "*5=" + (x * 5)); System.out.println(x + "*6=" + (x * 6)); System.out.println(x + "*7=" + (x * 7)); System.out.println(x + "*8=" + (x * 8)); System.out.println(x + "*9=" + (x * 9)); System.out.println(); x = 5; System.out.println(x + "*1=" + (x * 1)); System.out.println(x + "*2=" + (x * 2)); System.out.println(x + "*3=" + (x * 3)); System.out.println(x + "*4=" + (x * 4)); System.out.println(x + "*5=" + (x * 5)); System.out.println(x + "*6=" + (x * 6)); System.out.println(x + "*7=" + (x * 7)); System.out.println(x + "*8=" + (x * 8)); System.out.println(x + "*9=" + (x * 9)); System.out.println(); x = 6; System.out.println(x + "*1=" + (x * 1)); System.out.println(x + "*2=" + (x * 2)); System.out.println(x + "*3=" + (x * 3)); System.out.println(x + "*4=" + (x * 4)); System.out.println(x + "*5=" + (x * 5)); System.out.println(x + "*6=" + (x * 6)); System.out.println(x + "*7=" + (x * 7)); System.out.println(x + "*8=" + (x * 8)); System.out.println(x + "*9=" + (x * 9)); System.out.println(); x = 7; System.out.println(x + "*1=" + (x * 1)); System.out.println(x + "*2=" + (x * 2)); System.out.println(x + "*3=" + (x * 3)); System.out.println(x + "*4=" + (x * 4)); System.out.println(x + "*5=" + (x * 5)); System.out.println(x + "*6=" + (x * 6)); System.out.println(x + "*7=" + (x * 7)); System.out.println(x + "*8=" + (x * 8)); System.out.println(x + "*9=" + (x * 9)); System.out.println(); x = 8; System.out.println(x + "*1=" + (x * 1)); System.out.println(x + "*2=" + (x * 2)); System.out.println(x + "*3=" + (x * 3)); System.out.println(x + "*4=" + (x * 4)); System.out.println(x + "*5=" + (x * 5)); System.out.println(x + "*6=" + (x * 6)); System.out.println(x + "*7=" + (x * 7)); System.out.println(x + "*8=" + (x * 8)); System.out.println(x + "*9=" + (x * 9)); System.out.println(); x = 9; System.out.println(x + "*1=" + (x * 1)); System.out.println(x + "*2=" + (x * 2)); System.out.println(x + "*3=" + (x * 3)); System.out.println(x + "*4=" + (x * 4)); System.out.println(x + "*5=" + (x * 5)); System.out.println(x + "*6=" + (x * 6)); System.out.println(x + "*7=" + (x * 7)); System.out.println(x + "*8=" + (x * 8)); System.out.println(x + "*9=" + (x * 9)); System.out.println(); }
 
너무 길다. 반복문을 쓰자.
 
public static void main(String[] args) { for (int x = 2; x <= 9; x++) { System.out.println(x + "*1=" + (x * 1)); System.out.println(x + "*2=" + (x * 2)); System.out.println(x + "*3=" + (x * 3)); System.out.println(x + "*4=" + (x * 4)); System.out.println(x + "*5=" + (x * 5)); System.out.println(x + "*6=" + (x * 6)); System.out.println(x + "*7=" + (x * 7)); System.out.println(x + "*8=" + (x * 8)); System.out.println(x + "*9=" + (x * 9)); System.out.println(); } }
 
반복문을 활용해서 x 를 2~9까지 반복했다.
 
더 줄일 수 있을 것 같다.
 
public static void main(String[] args) { for (int x = 2; x <= 9; x++) { for (int i = 1; i <= 9; i++) { System.out.println(x + "*" + i + "=" + (x * i)); } System.out.println(); } }
반복문 두 번으로 구구단을 완성했다.
Share article

{CODE-RYU};