简体   繁体   English

如何在Java中打印以int为参数并返回double值的静态方法

[英]How to print a static method in java that takes an int as parameter and returns a double

I'm having difficulty trying to print the result of the static method calcArea, which takes the int radius as parameter to calculate the area of a circle and returns the area. 我在尝试打印静态方法calcArea的结果时遇到困难,该方法将int半径作为参数来计算圆的面积并返回该面积。

Here's the code below 👇 Any help would be appreciated. 这是下面的代码👇任何帮助将不胜感激。

public class CircleArea {

    public int radius;

     public static void main(String[] args) {

          Scanner input = new Scanner (System.in);
          System.out.print("Enter the radius of the circle: ");

         int radius = input.nextInt();

          System.out.print("The area of the circle is:" + calcArea()); <-- ERROR HERE
     }

      public static double calcArea(int radius){          

          double  area = Math.PI * Math.pow(radius, 2);
          return area;          

      }
}

您对calcArea调用需要传入一个参数。可能是calcArea(radius)

The function calcArea() takes the value of radius and then returns area. 函数calcArea()获取radius的值,然后返回area。 To do this, you need to pass an argument to calcArea(). 为此,您需要将一个参数传递给calcArea()。 So, your code should be like this: 因此,您的代码应如下所示:

    System.out.print("The area of the circle is:" + calcArea(radius));

The error you're getting clearly points out that you're missing an argument. 您得到的错误清楚地表明您缺少参数。

call method calcArea, you need give a parameter,Here are the correct example" 调用方法calcArea,您需要提供一个参数,这里是正确的示例”

public static void main(String[] args) {
      Scanner input = new Scanner (System.in);
      System.out.print("Enter the radius of the circle: ");
      int radius = input.nextInt();
      System.out.print("The area of the circle is:" + calcArea(radius));
 }

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

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