2 回答
TA贡献1818条经验 获得超3个赞
Circle.java
/**
* @Description TODO
* @Author Becolette
* @Date 2017-4-11 下午09:13:39
* @Modified By Becolette at 2017-4-11 for TODO
*/
public class Circle {
private double radius; // 半径
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public double getArea(double radius) {
return Math.PI * radius * radius;
}
public double getLength(double radius) {
return 2 * Math.PI * radius;
}
}
Test1.java
import java.text.DecimalFormat;
import java.util.Scanner;
/**
* @Description TODO
* @Author Becolette
* @Date 2017-4-9 下午07:52:35
* @Modified By Becolette at 2017-4-9 for TODO
*/
public class Test1 {
/**
* @Description TODO
* @Author Becolette
* @Date 2017-4-9 下午07:52:35
* @Modified By Becolette at 2017-4-9 for TODO
*/
public static void main(String[] args) {
Circle circle = new Circle();
DecimalFormat df = new DecimalFormat("#.00");
Scanner input = new Scanner(System.in);
System.out.println("输入半径:");
double r = input.nextDouble();
System.out.print("周长=" + df.format(circle.getLength(r)) + " 面积=" + df.format(circle.getArea(r)));
System.out.println("周长=" + df.format(2 * circle.getLength(r)) + " 面积=" + df.format(2 * circle.getArea(r)));
}
}
TA贡献2036条经验 获得超8个赞
public class Circle { private Double radius; public Double getArea() { if (radius == null || radius <= 0 ) { throw new RuntimeException( "radius不合法" ); } return Math.PI * radius * radius; } public Double getLength() { if (radius == null || radius <= 0 ) { throw new RuntimeException( "radius不合法" ); } return Math.PI * 2 * radius; } public Double getRadius() { return radius; } public void setRadius(Double radius) { this .radius = radius; } } |
import java.util.Scanner; public class App { public static void main(String[] args) { Scanner input = new Scanner(System.in); Circle circle = new Circle(); while ( true ) { Double radius = input.nextDouble(); circle.setRadius(radius); System.out.println(String.format( "%.2f %.2f" , circle.getArea(), circle.getLength())); circle.setRadius(radius * 2 ); System.out.println(String.format( "%.2f %.2f" , circle.getArea(), circle.getLength())); } } } |
添加回答
举报