使用方法无效的形状区域

Area of shapes using methods not working

这里的想法是获取用户输入(基本上是形状的名称)并根据该输入初始化其中一个方法。例如,如果有人进入 Square,我希望 squareArea 方法得到初始化。但那是行不通的。

    import java.util.Scanner;

    class Shapes {
    //Scan1 = scanner for integers
    Scanner Scan1 = new Scanner(System.in);
    //Method for finding out area of rectangle
void rectangleArea() {
    System.out.println("Enter dimensions");
    System.out.print("Length: ");
    float length = Scan1.nextInt();
    System.out.println("");
    System.out.print("Breadth: ");
    float breadth = Scan1.nextInt();
    System.out.println("The area of the is: " + length * breadth);
}
   //method for finding out area of square
void squareArea() {
    System.out.println("Enter dimensions");
    System.out.print("Length: ");
    float length = Scan1.nextInt();
    System.out.println("The area of the is: " + length * length);
}

}

public class AreaOfShapes {
public static void main(String[] args) {
    //Scan2 = scanner for strings
    Scanner Scan2 = new Scanner(System.in);
    Shapes Square = new Shapes();
    Shapes Rectangle = new Shapes();
    System.out.println("Which shape would you like to know the area of?");
    String Shape = Scan2.nextLine();
    /*want to compare input (which in this case is Shape) to a string. So for example, if someone types in Square, I want squareArea to get
    activated*/
    if (Shape.equals(Square)) {
        Square.squareArea();
     //not working
    } else if (Shape.equals(Rectangle)) {
        Rectangle.rectangleArea();
    }

    else {
        System.out.println("The shape you've entered does not exist in our database");
    }
}

}

我认为您是在谈论调用方法,而不是初始化方法。我真的不明白哪个部分不起作用,但是如果这个 ( if (Shape.equals(Square)) { Square.squareArea(); //not working ) 是效果不佳的部分,这是因为第一个 Square 应该这样写:"Square"(以便它可以是一个字符串,您可以将其与扫描仪输入进行比较)。如果其他方法不起作用,请更具体一些,我会尽力提供更多帮助。