Methods 使用不起作用的方法的形状区域

Methods 使用不起作用的方法的形状区域,methods,area,Methods,Area,这里的想法是获取用户输入的形状名称,并根据该输入初始化其中一个方法。例如,如果有人输入Square,我希望squareArea方法得到初始化。但这不起作用 import java.util.Scanner; class Shapes { //Scan1 = scanner for integers Scanner Scan1 = new Scanner(System.in); //Method for finding out area of rectan

这里的想法是获取用户输入的形状名称,并根据该输入初始化其中一个方法。例如,如果有人输入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);
}
}


}

我认为您谈论的是调用方法,而不是初始化它们。我真的不明白哪个部分不工作,但如果这个if Shape.equalsSquare{ 平方面积;
//不工作是指工作不好的部分,因为第一个正方形应该这样写:Square,这样它可以是一个字符串,可以与扫描仪输入进行比较。如果其他东西不工作,请更具体一点,我将尝试提供更多帮助。

最终应该是这样的:If Shape.equalsquare{Square.squareArea;}引号。这就是我所缺少的。我现在觉得很愚蠢。谢谢你帮我。现在它工作得很好。很高兴能帮上忙,这是我有史以来的第一个答案:祝你学习顺利!对不起,你能接受我的答案吗?这对我们双方都有好处。谢谢你。我实际上不知道怎么做。但如果你能告诉我怎么做,我会的。
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");
    }
}