2012-10-08 1 views
-1

드라이버 프로그램을 작성하면 사용자가 원의 사양을 입력하라는 메시지를 표시하고 원 객체를 생성 한 다음 원의 영역을 출력합니다. 프로그램에서 circle 클래스가 던진 IllegalDimesnsionException 오류를 적절히 처리해야합니다.throw 된 예외 오류를 사용하여 사용자에게 묻는 드라이버 프로그램 만들기

import java.util.Scanner; 

//A = C2/4π 

public class Driver { 
    public static void main(String[] args) { 
     String know; 
     // area of a circle 
     int Circumference; 
     int Diameter; 
     int Radius; 

     // pie of a circle 
     double pie; 
     double area; 

     // ask user for input 
     Scanner scanner = new Scanner(System.in); 
     System.out 
       .println("Do you know the Diameter, Circumference or the Radius of the circle?"); 
     know = scanner.nextLine(); 
     if (know.equals("Diameter")) { 
      System.out.println("What is the Diameter of the circle??"); 
      Diameter = scanner.nextInt(); 
      // Get area A = (π/4) × D2 

      pie = 3.14; 
      area = (pie/4) * (Diameter^2); 
      System.out.println("The area of the circle is: " + area); 
     } 
     if (know.equals("Radius")) { 
      System.out.println("What is the radius of the circle??"); 
      Radius = scanner.nextInt(); 
      // Get area A = π × r2 

      pie = 3.14; 
      area = pie * (Radius^2); 
      System.out.println("The area of the circle is: " + area); 
     } 

     if (know.equals("Circumference")) { 
      System.out.println("What is the circumference of the circle??"); 
      Circumference = scanner.nextInt(); 

      // Get area 
      pie = 3.14; 
      area = (Circumference^2)/4 * pie; 
      System.out.println("The area of the circle is: " + area); 
     } 
    } 
} 

난 그냥 예외 오류를 처리 할 수있는 프로그램을하는 데 도움이 필요 :

그래서 이것은 내가 지금까지있는 것입니다. 프로그램을 더 잘 만들기위한 다른 조언.

+0

우리는 숙제를하고 있습니까? –

+0

... [숙제 질문은 공식적으로 권장되지 않습니다] (http://meta.stackexchange.com/questions/147100/the-homework-tag-is-now-official-deprecated) – Augusto

+3

@Augusto - 숙제 태그! = 숙제에 관한 질문. –

답변

1

당신이 검토/의견을 찾고 있다면, 다음과 같이 몇 가지 발견하십시오 : 반경, 직경을 선언하고 (이 광석 의미가로)와 같은 2

  • 변경을 원주

    1. 더 나은을 "Diameter = scanner.nextInt();"와 같은 행 상이한 수치를 예를 들어 계산할

      boolean validInput = false; 
      do{ 
          try{ 
           diameter = scanner.nextDouble(); 
           validInput = true; 
          }catch(InputMismatchException ime){ 
           System.out.println("Invalid input. Please try again"); 
          } 
          }while(validInput == false); 
      
    2. 2 작은 방법 (모듈)을 생성 : 다음과 같이 사용자가 잘못된 입력을 처리 할

      private double calculateAreaUsingRadius(double radius) 
               throws IllegalDimesnsionException; 
      private double calculateAreaUsingCircumference(double circumference) 
               throws IllegalDimesnsionException; 
      
    3. 전화 예외 처리와 아래와 같이 메인 프로그램에서 이러한 방법 :이 프로그램을 개선하는 데 도움이됩니다

      try{ 
           area = calculateAreaUsingRadius(radius); 
      }catch(IllegalDimesnsionException){ 
           System.out.println("Invalid Radius Dimension"); 
      } 
      
      
      try{ 
           area = calculateAreaUsingRadius(diameter/2); 
      }catch(IllegalDimesnsionException){ 
           System.out.println("Invalid Diameter Dimension"); 
      } 
      
      
      try{ 
           area = calculateAreaUsingCircumference(circumference); 
      }catch(IllegalDimesnsionException){ 
           System.out.println("Invalid Circumference Dimension"); 
      } 
      

    희망.

  • 관련 문제