【发布时间】:2014-05-11 13:32:28
【问题描述】:
我正在练习我的 Java 书籍中的一些工作,但在获取使用变量进行计算的方法时遇到了问题。请注意,这是一项正在进行的工作,我只是想让它使用 circleArea 方法来计算圆的面积。这是必要的代码:
public class Geometry
{
public static void printMenu()
{
System.out.println("This is a geometry calculator\nChoose what you would like to calculate" + "\n1. Find the area of a circle\n2. Find the area of a rectangle\n3."
+ " Find the area of a triangle\n4. Find the circumference of a circle."
+ "\n5. Find the perimeter of a rectangle\n6. Find the perimeter of a triangle"
+ "\nEnter the number of your choice:");
}
public static void circleArea(double area)
{
System.out.println(Math.PI*Math.pow(radius, 2));
}
public static void main(String[] args)
{
int choice; //the user's choice
double value = 0; //the value returned from the method
char letter; //the Y or N from the user's decision to exit
double radius; //the radius of the circle
double length; //the length of the rectangle
double width; //the width of the rectangle
double height; //the height of the triangle
double base; //the base of the triangle
double side1; //the first side of the triangle
double side2; //the second side of the triangle
double side3; //the third side of the triangle
}
}
【问题讨论】:
-
您遇到的具体问题是什么?
-
声明到 main 中的任何变量都不能通过类中的其他函数访问。
-
家庭作业问题通常在这里做得不好,但您遇到的一个明显问题是您的
circleArea将area作为参数,而不是作为结果返回它。public static double circleArea(double radius)更像是您想要的声明(尽管在您当前的代码中,您只是发出结果而不是返回结果,因此如果您想这样做,您可以保留void)。 -
这不是家庭作业,仅供个人练习。使用的变量是本书提供的变量。我想我只是想知道如何让我的方法使用半径变量。
标签: java variables methods scope