类型的方法__;未定义____

ovfsdjhp  于 2021-08-20  发布在  Java
关注(0)|答案(2)|浏览(707)

好的,我有一个家庭作业,我很难调用另一个类中的主类的方法。
基本上,“test”方法在landenclosure.java类中,我试图在我的主类landandeat.java上调用它
它们都在同一个包中:
形象
这是我试图调用该方法的主类:

  1. public class landAndEat {
  2. public static void main(String[] args) {
  3. test();
  4. } //end class
  5. } //end main

这是创建方法的类:

  1. import java.util.Scanner;
  2. public class landEnclosure {
  3. public void test() {
  4. double area, ratioA = 0, ratioB = 0, x, l, w, perimeter;
  5. Scanner input = new Scanner(System.in);
  6. System.out.println("What area do you need for your enclosure in square feet?");
  7. area = input.nextDouble();
  8. if( area > 0 && area <= 1000000000) { //Input specification 1
  9. System.out.println("What is the ratio of the length to the width of your enclosure?");
  10. ratioA = input.nextDouble();
  11. ratioB = input.nextDouble();
  12. }
  13. else
  14. System.out.println("It needs to be a positive number less than or equal to 1,000,000,000!");
  15. if(ratioA > 0 && ratioA < 100 && ratioB > 0 && ratioB < 100) { //Input specification 2
  16. x = Math.sqrt(area/(ratioA*ratioB));
  17. l = ratioA * x;
  18. w = ratioB * x;
  19. perimeter = (2 * l) + (2* w);
  20. System.out.println("Your enclosure has dimensions");
  21. System.out.printf("%.2f feet by %.2f feet.\n", l, w);
  22. System.out.println("You will need " + perimeter + " feet of fence total");
  23. }
  24. else
  25. System.out.println("The ratio needs to be a positive number!");
  26. }
  27. } //end class
qxsslcnc

qxsslcnc1#

在java中,唯一顶级的“东西”是类(以及类似的东西,如接口和枚举)。功能不是顶级的“东西”。它们只能存在于类中。因此,要调用它,您需要遍历该类,或者遍历该类的对象。
从您编写的代码来看,test似乎是一种非静态方法。在这种情况下,您需要从该类创建一个对象,并在其上运行该方法:

  1. landEnclosure l = new landEnclosure();
  2. l.test();

然而,您的意图似乎是让“测试”成为一种静态方法。在这种情况下,将其声明为静态,并以这种方式调用:

  1. landEnclosure.test();

另一方面,java中的惯例是先用大写字母命名类:

  1. class LandEnclosure {
avkwfej4

avkwfej42#

除了创建新示例的明显建议之外 landEnclosure ,你也可以
function static 并致电:

  1. landEnclosure.test();

相关问题