java 求圆的半径、直径、面积和周长
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28688716/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Finding the radius, diameter, area and circumference of a circle
提问by james13
So I'm still new to programming and I don't know if this is all correct or not, but I'm trying to find the area circumference of a circle with a given radius.
所以我还是编程新手,我不知道这是否全部正确,但我试图找到给定半径的圆的周长。
So far I have this:
到目前为止,我有这个:
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;
}
}
I also have this other part too...
我也有这个其他部分...
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) {
}
}
I don't know if this is right or not, but it compiles just fine but it doesn't print anything out when I run it. What am I doing wrong???
我不知道这是否正确,但它编译得很好,但是当我运行它时它没有打印任何内容。我究竟做错了什么???
回答by SabaRish
the code has few mistakes . it has to be like this
代码几乎没有错误。它必须是这样的
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;
}
}
main class has to be like this
主类必须是这样的
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());
}
}
mistakes you have made 1) your code has to be in main method .2) the constructor parameter has to be set to the class variable.
您所犯的错误 1) 您的代码必须在 main 方法中 .2) 必须将构造函数参数设置为类变量。
回答by bumbumpaw
In the Java language, when you execute a class with the Java interpreter, the runtime system starts by calling the class's main() method.
在 Java 语言中,当您使用 Java 解释器执行类时,运行时系统通过调用类的 main() 方法启动。
You should put some code in this block
你应该在这个块里放一些代码
public static void main(String[] args) {
}