编译main时出错,错误来自我的类文件。 这样做的目的是创建一个程序,要求用户创建一个圆,然后返回给用户区域,直径和周长。
java.util.Scanner;
public class Driver {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
double circumference;
double radius = 5.7;
double pi = 3.14159;
double area = 0;
Circle circleobject = new Circle();
//Get radius
System.out.println("What is the circle's Radius?");
radius = keyboard.nextDouble();
if (radius == 0.0) {
radius = 5.7;
}
System.out.println("Creating a circle object.");
//Creating the class object
Circle circle = new Circle();
System.out.println("Your circle's area: " + Circle.getArea());
System.out.println("Your circle's Diameter " + Circle.getDiameter());
System.out.println("Your circle's Circumference " + Circle.getCircumference());
}
这是我的班级文件
public class Circle {
double circumference;
double radius = 0.0;
double pi = 3.14159;
double area = 0;
public void setRadius() {
radius = radius;
}
public void radius() {
radius = 0.0;
}
public void getArea(double area) {
area = 0;
area = pi * radius * radius;
}
public void getDiameter(double diameter) {
diameter = radius * 2;
}
public void getCircumference(double circumference) {
circumference = 2 * pi * radius;
}
}
这是我编译main时出现的错误。
Driver.java:38: error: method getCircumference in class Circle cannot be applied to given types;
System.out.println("Your circle's Circumference " + Circle.getCircumference());
^
required: double
found: no arguments
reason: actual and formal argument lists differ in length
1 error
答案 0 :(得分:4)
你的方法
public void getCircumference(double circumference)
{
circumference = 2 * pi * radius;
}
应改写为:
public double getCircumference() {
return 2 * pi * radius;
}
使用Java约定,您应该以下列方式使用getter和setter:
private xxx myAttribute;
// Here you can check if passed values are correct, for example if you expect a negative value or not.
public void setMyAttribute(xxx a) {
this.myAttribute=a;
}
// It just returns the current value
public xxx getMyAttribute() {
return myAttribute;
}
答案 1 :(得分:2)
您应该替换此方法:
public void getCircumference(double circumference) {
circumference = 2 * pi * radius;
}
由此:
public double getCircumference() {
return 2.0 * pi * radius;
}
即。它不应该采用任何参数,并返回周长作为结果值。
修改强>
您还应修改getArea
和getDiameter
:
public double getArea() {
return pi * radius * radius;
}
public double getDiameter() {
return radius * 2;
}
<强> EDIT2 强>
如果我这样说,您应该进一步修改您的代码。首先,您的Circle
类应该如下所示:
PI
是一个静态常量(对于Circle的每个实例都相同)radius
参数的构造函数,这样您就可以实例化一个新的Circle
,提供radius
radius
和PI
getRadius()
方法以提供对radius
值的公开访问权。有一个我会做的例子:
public class Circle {
private static final double PI = 3.14159;
private double radius = 0.0;
public Circle(double radius){
setRadius(radius);
}
public void setRadius(double radius) {
this.radius = radius;
}
public double getRadius() {
return radius;
}
public double getArea() {
return PI * radius * radius;
}
public double getDiameter() {
return 2.0 * radius;
}
public double getCircumference() {
circumference = 2.0 * PI * radius;
}
}
因此,您的主程序将如下所示:
java.util.Scanner;
public class Driver {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
double radius;
Circle circleobject = new Circle(radius);
//Get radius
System.out.println("What is the circle's Radius?");
radius = keyboard.nextDouble();
if (radius == 0.0) {
radius = 5.7;
}
System.out.println("Creating a circle object.");
//Creating the object
Circle circle = new Circle(radius);
System.out.println("Your circle's area: " + circle.getArea());
System.out.println("Your circle's Diameter " + circle.getDiameter());
System.out.println("Your circle's Circumference " + circle.getCircumference());
}
}
我删除了一些无用的变量(circumference
,pi
和area
),修改了实例化Circle
对象的方式(使用新的构造函数)并修改了调用到getArea()
,getDiameter()
,getCircumference()
(您将它们称为静态方法,但您需要在实例circle
上调用它们,因为它们不是静态方法)。
我建议您查看一下Understanding Instance and Class Members和Returning a Value from a Method Oracle的文章,更一般地看一下整个Classes and Objects chapter。