所以我还是编程的新手,我不知道这是否正确,但我试图找到一个给定半径的圆的面积。
到目前为止,我有这个:
public class Circle {
private double radius;
public Circle(double r) {
}
public double getRadius() {
return radius;
}
public void setRadius(double r) {
}
public double diameter() {
double diameter = radius * radius;
return diameter;
}
public double area() {
double area = Math.PI * (radius * radius);
return area;
}
public double circumference() {
double circumference = 2 * Math.PI * radius;
return circumference;
}
}
我也有这个其他部分......
public class CircleTest {
private static void circleTest (int r) {
Circle circleTest = new Circle(-2);
System.out.printf("Parameter: %d%n", r);
System.out.printf("Radius: %.1f %n", circleTest.getRadius());
System.out.printf("Diameter: %.1f %n", circleTest.diameter());
System.out.printf("Area: %.1f %n", circleTest.area());
System.out.printf("Circumference: %.1f %n", circleTest.circumference());
}
public static void main(String[] args) {
}
}
我不知道这是否正确,但它编译得很好,但是当我运行它时它不打印任何东西。我做错了什么???
答案 0 :(得分:0)
代码几乎没有错误。它必须像这样
public class Circle {
private double radius;
public Circle(double r) {
radius = r;
}
public double getRadius() {
return radius;
}
public void setRadius(double r) {
}
public double diameter() {
double diameter = radius * radius;
return diameter;
}
public double area() {
double area = Math.PI * (radius * radius);
return area;
}
public double circumference() {
double circumference = 2 * Math.PI * radius;
return circumference;
}
}
主要类必须像这样
public class CircleTest {
public static void main(String[] args) {
Circle circleTest = new Circle(-2);
System.out.printf("Parameter: %d%n", r);
System.out.printf("Radius: %.1f %n", circleTest.getRadius());
System.out.printf("Diameter: %.1f %n", circleTest.diameter());
System.out.printf("Area: %.1f %n", circleTest.area());
System.out.printf("Circumference: %.1f %n", circleTest.circumference());
}
}
你犯的错误1)你的代码必须在main方法中.2)构造函数参数必须设置为类变量。
答案 1 :(得分:-1)
在Java语言中,当您使用Java解释器执行类时,运行时系统首先调用类的main()方法。
您应该在此块中添加一些代码
public static void main(String[] args) {
}