어떤 숫자의 값과도 동등한 프로그램을 작성하려고합니다. 성공적으로 수행 한 지수가 0보다 작고 예외 처리를 구현한다고 가정합니다. 값이 너무 커서 무한대로 출력 할 수 없습니다.무한대에 대한 Java 예외 처리
이public class power
{
// instance variables - replace the example below with your own
public static double Power(double base, int exp) throws IllegalArgumentException
{
if(exp < 0){
throw new IllegalArgumentException("Exponent cannot be less than zero");
}
else if(exp == 0){
return 1;
}
else{
return base * Power(base, exp-1);
}
}
}
Heres는 테스트 클래스 : heres는
public class powerTest
{
public static void main(String [] args)
{
double [] base = {2.0, 3.0, 2.0, 2.0, 4.0 };
int [] exponent = {10, 9, -8, 6400, 53};
for (int i = 0; i < 5; i++) {
try {
double result = power.Power(base[i], exponent[i]);
System.out.println("result " + result);
}
catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
}
catch (ArithmeticException e) {
System.out.println(e.getMessage());
}
}
}
}
시험의 출력 :
이result 1024.0
result 19683.0
Exponent cannot be less than zero
result Infinity
result 8.112963841460668E31
내 질문은 함수 전원을 포함
을 heres 내 전력 등급 뭔가를 처리 ArithmeticException 통해 뭔가 다른 말을 "결과 무한"얻을 수 있습니다. "Floating point Overflow"라인을 따라? 사전에
감사합니다.
당신이 묻는 것이 명확하지 않습니다. 결과가 무한 경우 예외를 throw 하시겠습니까? 결과가 무한인지 확인하는 방법을 알고 싶습니까? – Radiodef
결과가 무한 할 때 예외를 throw –
예외를 throw하는 방법을 잘 알고 있습니다. 아마도 문제를 편집하여 문제가 무엇인지 명확히 할 수 있습니다. – Radiodef