求圆形的面积,要求用户输入任何类型的数据后都要能得到最终的面积值。 分析: 1) 创建一个类,类名为Circle。 2) 编写有参方法1:求圆的面积,参数为圆的半径(int类型)。 3) 编写有参方法2:求圆的面积,方法名和方法1相同,参数为float类型。 4) 编写有参方法3:求圆的面积,方法名和方法1相同,参数为double类型。 5) 在main方法中实例化1个对象,分别调用3个方法计算圆的面积。
时间: 2023-05-28 11:04:51 浏览: 31
代码如下:
public class Circle {
public static void main(String[] args) {
Circle c = new Circle();
int radiusInt = c.getInputInt();
float radiusFloat = c.getInputFloat();
double radiusDouble = c.getInputDouble();
c.getArea(radiusInt);
c.getArea(radiusFloat);
c.getArea(radiusDouble);
}
public int getInputInt() {
Scanner sc = new Scanner(System.in);
System.out.println("请输入圆的半径(整数):");
int radius = sc.nextInt();
return radius;
}
public float getInputFloat() {
Scanner sc = new Scanner(System.in);
System.out.println("请输入圆的半径(浮点数):");
float radius = sc.nextFloat();
return radius;
}
public double getInputDouble() {
Scanner sc = new Scanner(System.in);
System.out.println("请输入圆的半径(双精度浮点数):");
double radius = sc.nextDouble();
return radius;
}
public void getArea(int radius) {
double area = Math.PI * radius * radius;
System.out.println("圆的面积为:" + area);
}
public void getArea(float radius) {
double area = Math.PI * radius * radius;
System.out.println("圆的面积为:" + area);
}
public void getArea(double radius) {
double area = Math.PI * radius * radius;
System.out.println("圆的面积为:" + area);
}
}
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)