我试图将一个双变量从一个方法调用到另一个方法

0 java variables methods call

如何在不在类中创建私有变量的情况下将length和width变量调用到getArea方法中,我这样做的方法是在方法运行一次之后再次运行该方法.我真的不喜欢这种方式,但这就是教授希望它在"面向对象编程"之前模拟时间的方式

/*This program allows the user to enter the length and widtch and receive the area
 of the rectangle*/

 import java.util.Scanner;

public class theRectangleCompany
{
  public static void main(String[] args)
  {
    System.out.print("This program will find an area of a Rectangle ");
    getLength();
    getWidth();
    getArea();
  }

  public static double getLength()
  {
    double length;
    Scanner keyboard = new Scanner(System.in);
    System.out.print("Please enter the length ");
    length = keyboard.nextDouble();
    return length;
  }

  public static double  getWidth()
  {
     double width;
     Scanner keyboard = new Scanner(System.in);
     System.out.print("Please enter the width ");
     width = keyboard.nextDouble();
     return width;
  }

  public static void getArea()
    {
      double length = getLength();
      double width = getWidth();
      double area = width * length;
      System.out.println("The area of the Rectangle is: " +area);

    }


}
Run Code Online (Sandbox Code Playgroud)

ash*_*ish 5

为什么要从main方法调用getLength()和getWidth().只需调用getArea()

public static void main(String[] args)
  {
    System.out.print("This program will find an area of a Rectangle ");
    getArea();
  }
Run Code Online (Sandbox Code Playgroud)