대학 생활/JAVA
[JAVA] 구구단 3단씩 출력(try-catch 사용)_01
opid
2013. 10. 8. 00:00
public class MainClass02 { public static void main(String[] args) { MultiplicationTable start = new MultiplicationTable(); start.inputValue(); start.outputResult(); // start.outputResult2(); } }
import java.util.Scanner; public class MultiplicationTable { int input1, input2; void inputValue() { // void inputValue() throws Exception{ int chk = 0; while (chk != 1) { try { // 입력 Scanner in = new Scanner(System.in); System.out.print("first value : "); this.input1 = in.nextInt(); System.out.print("second value : "); this.input2 = in.nextInt(); // 예외 발생시키기 if ((input1 > input2)) { throw new Exception("\n첫번째 값은 뒷자리보다 작은 수를 입력하시오.\n"); } else if ((input1 > 9) || (input2 > 9)) { throw new Exception("\n9 미만의 수를 입력하시오.\n"); } // 예외가 일어나지 않아야 밑으로 이동 chk = 1; } catch (Exception e) { System.err.println(e.getMessage()); // System.err.println(e); } } } void outputResult() { for (int dan = input1; dan <= input2; dan += 3) { for (int line = 1; line < 10; line++) { System.out.printf(dan > input2 ? " " : "%d x %d = %2d ", dan, line, (dan * line)); System.out.printf(dan + 1 > input2 ? " " : "%d x %d = %2d ", dan + 1, line, ((dan + 1) * line)); System.out.printf(dan + 2 > input2 ? "\n" : "%d x %d = %2d\n", dan + 2, line, ((dan + 2) * line)); } System.out.println(""); } } }