[java]代码库
package homework;
import java.nio.channels.ScatteringByteChannel;
import java.security.PublicKey;
import java.util.Scanner;
class polygon {
private double a, n;
polygon(double an, double nn) {
a = an;
n = nn;
}
private double angle() {
return (((n-2)*180)/n)/2;
}
private double trangle() {
double a1=a/2;
double c = a1* Math.tan(angle()*(Math.PI/180));
return (float)c*a*0.5;
}
double area() {
return n * trangle();
}
}
class main2 {
public static void main(String[] args) {
System.out.println("请输入边长和有几条边");
Scanner reader = new Scanner(System.in);
double a =reader.nextDouble();
System.out.println("边长为 "+a+"cm");
double n = reader.nextDouble();
System.out.println("边有 "+n+"条");
if (a<=0 || n<3)
System.out.println("输入错误");
polygon s = new polygon(a, n);
System.out.println("面积为"+s.area()+"cm^2");
}
}
[代码运行效果截图]