用Java计算圆的面积

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19756552/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 19:56:59  来源:igfitidea点击:

Calculating the area of a circle in Java

javaconstructorgeometry

提问by Paul Kappock

I need to write a program to calculate the area of a circle and I seem to have everything right except when I run the program and input the values the area calculation comes up as zero.

我需要编写一个程序来计算圆的面积,我似乎一切都正确,除非我运行该程序并输入面积计算结果为零的值。

public class Circle {

    private double radius;
    private double area;

    public Circle() {
        radius = 0;
        area = 0;
    }

    public Circle(double radius) {
        this.radius = radius;
    }

    public double getRadius() {
        return radius;
    }

    public void setRadius(double radius) {
        this.radius = radius;
    }

    public double getArea() {
        return area;
    }

    public void setArea(double area) {
        area = radius * radius * Math.PI;
    }

    public String toString() {
        return "The radius of the circle is: " + radius + ", and the area is: " + area;
    }
}

What do I need to change so that when my test code calls the toString it will output a calculated area?

我需要更改什么才能在我的测试代码调用 toString 时输出计算区域?

采纳答案by sdanzig

You should have a method calculate area based on the current radius. Area should not be set.

您应该有一种基于当前半径计算面积的方法。不应设置区域。

public class Circle {
    private double radius;

    public Circle() {
        radius = 0;
    }

    public Circle(double radius) {
        this.radius = radius;
    }

    public double getRadius() {
        return radius;
    }

    public void setRadius(double radius) {
        this.radius = radius;
    }

    public double getArea() {
        return calculateArea();
    }

    private double calculateArea() {
        return radius * radius * Math.PI;
    }

    public String toString() {
        return "The radius of the circle is: " + radius + ", and the area is: "
                + calculateArea();
    }
}

If you did wish to store area in a variable, you should update it when you set the radius. It shouldn't be independently set from "setArea". Otherwise, you're vulnerable to inconsistency. Also, a note from Josh Bloch's "Effective Java". While your toString should take advantage of this "calculated area" rather than replicating the calculation, you shouldn't have toString call anything in your public API. That would be an issue if, for instance, you overrode getArea, which would mean it would behave differently than your Circle.toString expects. That's why I put the private "calculateArea" in there.

如果您确实希望将区域存储在变量中,则应在设置半径时更新它。它不应该从“setArea”中独立设置。否则,您很容易出现不一致的情况。此外,还有来自 Josh Bloch 的“Effective Java”的注释。虽然您的 toString 应该利用这个“计算区域”而不是复制计算,但您不应该在公共 API 中调用任何 toString。例如,如果您覆盖了 getArea,这将是一个问题,这意味着它的行为将与您的 Circle.toString 预期不同。这就是为什么我把私有的“calculateArea”放在那里。

回答by Michael Yaworski

You originally set the areato be 0. You created a method to change that, but never called it. So call it. Change this:

您最初将 设置area为 0。您创建了一个方法来更改它,但从未调用过它。所以叫它。改变这个:

public String toString() {
    return "The radius of the circle is: " + radius + ", and the area is: " + area;
}

to this:

对此:

public String toString() {
    setArea(area); // change the value of the area
    return "The radius of the circle is: " + radius + ", and the area is: " + area;
}

回答by satya

Pass radiusinstead of areain the setAreamethod:

通过radius而不是areasetArea方法中:

public void setArea( double radius )  
{
    area = (radius)*(radius)*Math.PI;
}   

for complete code- http://pastebin.com/jggRrUFd

完整代码 - http://pastebin.com/jggRrUFd

回答by HAK

This is my version of the Circle_Math Class, with entry validation and with the minimum number of lines / operations and memory space to get the job done. Please leave a comment if you want.

这是我的 Circle_Math 类版本,具有条目验证和最少的行/操作数和内存空间来完成工作。如果您愿意,请发表评论。

public class circle {
    static double rad;

    public circle() {
        rad = 0;
    }

    public static void setRad() {
        Scanner sc = new Scanner(System.in);

        do {
            while (!sc.hasNextDouble()) {
                sc.next();// this code is to skip the exception created 
            }
            rad = sc.nextDouble();
        } while (rad < 0);

        System.out.println("radius value is:" + rad);

    }

    public static double getCirclearea() {
        return rad * rad * Math.PI;
    }

    public static double getCircumference() {
        return 2 * Math.PI * rad;
    }

}

回答by Fadi

import java.util.Scanner;
public class Circle {
    // variable PI is readable only;
    // constant value
    public static final double PI = 3.14;
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);

        System.out.print("Enter raduis: ");
        double raduis = input.nextDouble();

        double area = PI * raduis * raduis;
        System.out.print("Circle area = " + area);

    }

}

回答by Neeraj Gahlawat

public static double getAreaOfCircle(int radius){
        return Math.PI*radius*radius;
    }