简体   繁体   English

当我尝试调用方法“ calcArea”时,出现错误消息“无法从静态上下文中引用非静态方法calcArea()”

[英]When I try to invoke method “calcArea”, error message “non-static method calcArea() cannot be referenced from a static context” appears

I've messed around with this for an hour now and can't get it to work. 我已经搞砸了一个小时,无法正常工作。 I've also looked this question up but the wording used in answers I've found haven't helped me at all. 我也查询了这个问题,但是我发现答案中使用的措辞根本没有帮助我。

Any help would be much appreciated. 任何帮助将非常感激。

Also, the invocation in question is at the very end of the program, inside main . 同样,有问题的调用位于程序的最后,即main内

import java.util.Scanner;
public class Area {

Scanner input = new Scanner (System.in);
/**
 * @param args the command line arguments
 */
public static void areaTriangle (double height, double length){
   System.out.println((height * length) * .5);
}

public static void areaRectangle (double height, double length, 
        double width){
   System.out.println(height * length * width);
}

public static void areaCircle (double radius){
  System.out.println(Math.PI * (radius * radius));
}

  public void calcArea (){
    double i;
    System.out.println("Which shape's area would you like to calculate?: \n"
    + "Enter '1' for Triangle \n"
    + "Enter '2' for Rectangle \n"
    + "Enter '3' for Circle \n"
    + "Enter '0' to quit the program");

    i = input.nextDouble();

    if (i == 1)
    {
            System.out.print("Enter the height of your triangle: ");
            double height = input.nextDouble();

            System.out.print("Enter the height of your length: ");
            double length = input.nextDouble();

            areaTriangle(height, length);
    }
    else if ( i == 2)
    {
        System.out.print("Enter the height of your rectangle: ");
        double height = input.nextDouble();

        System.out.print("Enter the length of your rectangle: ");
        double length = input.nextDouble();

        System.out.print("Enter the width of your rectangle: ");
        double width = input.nextDouble();

        areaRectangle(height, length, width);
    }
    else if ( i == 3)
    {
        System.out.print("Enter the radius of your circle");
        double radius = input.nextDouble();

        areaCircle(radius);
    }
    else if ( i == 0)
        return;
    else
        System.out.println("Please input a number from 0 - 3");


  }

  public static void main(String[] args) {
    calcArea();
  }

}

put static in calcArea 将静态放在calcArea中

public static void calcArea ()

or you can do this in your main 或者你可以在你的主要

 public static void main(String[] args) {
     YourClassName variable= new YourClassName();
     variable.calcArea();
    }

just change the "YourClassName" to the name of your class and also you can change the variable object 只需将“ YourClassName”更改为您的的名称,还可以更改变量对象

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM