java - 为什么我的重载静态方法无法识别我的高度参数?

标签 java methods static overloading

因此,我在 AreaClient 类中使用了三个静态重载方法,它们从用户获取输入并将这些输入作为参数传递给下面的方法。由于某种原因,我似乎无法获得最后一个区域方法来将我的高度变量作为参数。我不断收到错误消息“找不到符号”。这些应该是重载方法,正如赋值所示。抱歉,如果这真的很简单,但我对编程还很陌生。这是我编写的代码。

import java.util.Scanner;    // Needed for the Scanner class

public class AreaClient {

public static void main(String[] args) {

 double circleRadius;           //input for radius of circle
 int width, length;             //input for rectangle width and length
 double cylinderRadius, height; //input for radius of a cylinder and hieght

 // Create a Scanner object for keyboard input.
 Scanner keyboard = new Scanner(System.in);

 // gathering input for radius of circle
 System.out.println("Enter radius of circle");
 circleRadius = keyboard.nextDouble();

 // input for width and length of rectangle
 System.out.println("Enter width of rectangle");
 width = keyboard.nextInt();
 System.out.println("Enter length of rectangle");
 length = keyboard.nextInt();

 // input for radius and hieght of cylinder
 System.out.println("Enter radius of cylinder");
 cylinderRadius = keyboard.nextDouble();
 System.out.println("Enter hieght of cylinder");
 height = keyboard.nextDouble();

 //returning area methods results and storing them in new variables
 double circleArea = area(circleRadius);
 int rectangleArea = area(width, length);
 double cylinderArea = area(cylinderRadius, height);

 //displaying results of methods
 System.out.println("The area of your circle is: " + circleArea);
 System.out.println("The area of your rectangle is: " + rectangleArea);
 System.out.println("The area of your cylinger is: " + cylinderArea);
}


//overloaded methods that take different inputs
public static double area(double r)
{
  return 3.14159265359 * Math.pow(r, 2);
}

public static int area(int w, int l)
{
  return w * l;
}

//actual method that doesn't recognize h inside
public static double area(double r, double h)
{
  return 2*3.14159265359 * Math.pow(r,2) + h (2*3.14159265359*r);
}


}

以及我收到的错误消息

AreaClient.java:54: error: cannot find symbol
  return 2*3.14159265359 * Math.pow(r,2) + h (2*3.14159265359*r);
                                           ^
symbol:   method h(double)
location: class AreaClient
1 error

谢谢大家。非常感谢任何帮助。

最佳答案

错误消息中的注意事项:

symbol:   method h(double)

为什么它要寻找一个名为 h() 的方法来接受 double 值?因为你告诉它:

h (2*3.14159265359*r)

h 不是一个方法,它只是一个值。您需要使用运算符将​​其连接到其他值。我认为你的意思是这样做:

h * (2*3.14159265359*r)

关于java - 为什么我的重载静态方法无法识别我的高度参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19692075/

相关文章:

JavaFX TextArea滚动条移动事件

java - 如何在 main 中使用带有数组的 JOptionPane.showInputDialog 方法?

java - 为什么我不能在 Java 中使用 "static import"和 "equals"方法?

PHP get_token_all 对象类型

swift - 如何在swift的泛型类中定义静态常量?

Java - 从组合对象调用 this.function()

java - 是否可以自动定位 LAN 中的 java RMI 注册表?

Java 传递参数数组

c# - 执行具有任何签名的方法的方法

java - JpaRepository 从 postgres 表中读取多次行