计算圆的面积,半径不能为零和负数
package com.entity;
public class Area {
public void Area(double r) throws Exception {
double pi=Math.PI;
if(r==0){
throw new Exception("半径不能为零");
}else if(r<0){
throw new Exception("半径不能为负数");
}else{
System.out.println("圆的面积:"+pi*r*r);
}
}
}
package com.test;
import com.entity.Area;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
System.out.println("输入半径");
int r= sc.nextInt();
Area area= new Area();
area.Area(r);
}
}