0

所以我在我的 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

多谢你们。任何帮助深表感谢。

4

2 回答 2

2

注意错误消息中:

symbol:   method h(double)

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

h (2*3.14159265359*r)

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

h * (2*3.14159265359*r)
于 2013-10-30T19:27:58.627 回答
1

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

return 2*3.14159265359 * Math.pow(r,2) + h * (2*3.14159265359*r);
于 2013-10-30T19:27:16.450 回答