为什么我的重载静态方法不会识别我的高度参数?

时间:2013-10-30 19:24:35

标签: java methods static overloading

所以我在我的AreaClient类中使用了三个静态的重载方法,这些方法从用户那里获取输入并将这些输入作为参数传递给下面的方法。出于某种原因,虽然我似乎无法获得最后一个区域方法来接受我的hieght变量作为参数。我一直收到一个错误,上面写着“无法找到符号”。这些应该是重载方法,正如赋值所说的那样。对不起,如果这很简单,但我对编程很新。这是我写的代码。

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

谢谢你们。非常感谢任何帮助。

2 个答案:

答案 0 :(得分:2)

请注意错误消息:

symbol:   method h(double)

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

h (2*3.14159265359*r)

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

h * (2*3.14159265359*r)

答案 1 :(得分:1)

我认为你的意思是:h * (2*3.14159265359*r)。没有运算符,Java认为您正在尝试调用名为h(double)

的方法
return 2*3.14159265359 * Math.pow(r,2) + h * (2*3.14159265359*r);