我创建了方法,但无法在 main 中调用它

I created the method but I can't call it in main

我想从 main 调用方法 calculationsMethod 来执行计算并显示结果,但我不知道如何进行。请帮忙。

import java.util.Scanner;
public class JavaHomework1 {

static void calculationsMethod() {
   Float perimeter = ((2*length) + (2*breadth));
   Float area = ((length*breadth));
   System.out.println("The Perimeter of the Rectangle is " + perimeter);
  }

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println("Input the length of the rectangle: ");
    Float length = input.nextFloat();

    System.out.println("Input the breadth of the rectangle: ");
    Float breadth = input.nextFloat(); 
    input.close();
    // I want to send length and breadth to the above method for the calculation
    calculationsMethod(); // then call the method for displaying the results
    
}

}

所以基本上你的变量只存在于main()的范围内,唯一缺少的是在方法中将它们声明为字段。

import java.util.Scanner;
public class Stack {

    //You did not declare them or call them to the method, 
    //so that's why your method didn't recognise length or breadth
static void calculationsMethod(Float length, Float breadth) {
   Float perimeter = ((2*length) + (2*breadth));
   Float area = ((length*breadth));
   System.out.println("The Perimeter of the Rectangle is " + perimeter);
  }

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println("Input the length of the rectangle: ");
    Float length = input.nextFloat();

    System.out.println("Input the breadth of the rectangle: ");
    Float breadth = input.nextFloat(); 
    input.close();
    //So basically your length and breadth only existed in the scope of main().
    calculationsMethod(length, breadth); 
    
}

}

这个有效: