JAVA 프로그래밍

문제

두 숫자를 입력받아 사칙연산을 수행하고 예외가 발생하면 이를 처리하는 문제입니다 이를 해결하는 프로그램의 다음 실행상태에 대해 빈칸을 채우세요 

프로그램의 실행순서 및 실행상태

1  public static void main( String args[] ) { 

4                                 scan.nextLine() ); 
사칙연산을 위한 첫번째 숫자를 입력하세요: a

3               Integer.parseInt(
                main()                              
scan  →  등          (Scanner)
   e  →  (NumberFormatException)

8   catch ( NumberFormatException e ) {

9    System.out.println( "숫자 형식 불일치" );
숫자 형식 불일치

14   finally {

15    System.out.println( "예외 실습 완료" );
예외 실습 완료

16  } 

1  public static void main( String args[] ) { 

4                                 scan.nextLine() ); 
사칙연산을 위한 첫번째 숫자를 입력하세요: 2

3               Integer.parseInt(

2    int num1 = 

6               scan.nextInt();
사칙연산을 위한 두번째 숫자를 입력하세요: b
                main()                              
scan  →  등          (Scanner)
num1
   e  →  (InputMismatchException)

8   catch ( NumberFormatException e ) {

10   catch ( InputMismatchException e ) {

11     System.out.println( "입력 자료형 불일치" );
입력 자료형 불일치

14   finally {

15    System.out.println( "예외 실습 완료" );
예외 실습 완료

16  } 

1  public static void main( String args[] ) { 

4                                 scan.nextLine() ); 
사칙연산을 위한 첫번째 숫자를 입력하세요: 2

3               Integer.parseInt(

2    int num1 = 

6               scan.nextInt();
사칙연산을 위한 두번째 숫자를 입력하세요: 0

5    int num2 =

7    System.out.println( num1 + " / " + num2 + " = " + ( num1 / num2 ) );
2 + 0 = 2
2 - 0 = 2
2 * 0 = 0
                main()                              
scan  →  등          (Scanner)
num1
num2
e  →  (ArithmeticException)

8   catch ( NumberFormatException e ) {

10   catch ( InputMismatchException e ) {

12   catch ( ArithmeticException e ) {

13    System.out.println( "산술 연산 오류" );
산술 연산 오류

14   finally {

15    System.out.println( "예외 실습 완료" );
예외 실습 완료

16  } 


프로그램 코드

	import java.util.*;
	
	public class NumberExceptions
	{  
1		public static void main( String args[] ) { 
			try {
				Scanner scan = new Scanner( System.in );
				System.out.print( "사칙연산을 위한 첫번째 숫자를 입력하세요: " );
2				int num1 = 
3				           Integer.parseInt(
4				                             scan.nextLine() ); 
				System.out.print( "사칙연산을 위한 두번째 숫자를 입력하세요: " );
5				int num2 =
6				           scan.nextInt();
				
				System.out.println( num1 + " + " + num2 + " = " + ( num1 + num2 ) );
				System.out.println( num1 + " - " + num2 + " = " + ( num1 - num2 ) );
				System.out.println( num1 + " * " + num2 + " = " + ( num1 * num2 ) );
7				System.out.println( num1 + " / " + num2 + " = " + ( num1 / num2 ) );
			}	
8			catch ( NumberFormatException e ) {
9				System.out.println( "숫자 형식 불일치" );
			}
10			catch ( InputMismatchException e ) {
11		 		System.out.println( "입력 자료형 불일치" );
			}
12			catch ( ArithmeticException e ) {
13				System.out.println( "산술 연산 오류" );
			} 
14			finally {
15				System.out.println( "예외 실습 완료" );
			} 
			
16		} 
	}