将上面的main函数改成这样该如何去编写代码import java.util.Scanner;public class Main{ public static void main(String[] args) { Square s = new Square(); Circle c = new Circle(); Scanner sc = new Scanner(System.in); double l = sc.nextDouble(); double r = sc.nextDouble(); System.out.println("正方形的边长:" + l); System.out.println("正方形的面积:" + s.area(l)); System.out.println("圆形的半径:" + r); System.out.println("圆形的面积:" + c.area(r)); }}/* 请在这里填写答案 */
时间: 2024-01-13 10:03:17 浏览: 64
好的,下面是修改后的代码:
Shape.java
```
public interface Shape {
double area(double len);
}
```
Square.java
```
public class Square implements Shape {
@Override
public double area(double len) {
return len * len;
}
}
```
Circle.java
```
public class Circle implements Shape {
@Override
public double area(double r) {
return Math.PI * r * r;
}
}
```
Main.java
```
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Square s = new Square();
Circle c = new Circle();
Scanner sc = new Scanner(System.in);
double l = sc.nextDouble();
double r = sc.nextDouble();
System.out.println("正方形的边长:" + l);
System.out.println("正方形的面积:" + s.area(l));
System.out.println("圆形的半径:" + r);
System.out.println("圆形的面积:" + c.area(r));
}
}
```
运行结果:
```
5
3
正方形的边长:5.0
正方形的面积:25.0
圆形的半径:3.0
圆形的面积:28.274333882308138
```
阅读全文