0

我有一个任务来创建一个 Circle 类,它使用半径创建一个圆,然后使用 get 来查找面积、直径等。

分配的下一步是“继续使用开始时创建的扫描仪对象,要求用户输入另一个半径值。” 起初我尝试复制+粘贴代码以询问用户半径并像我最初所做的那样分配给局部变量,我收到以下错误:“从未使用过分配的值。” 分配还说要更改圆实例的值,而不是创建一个新圆并为其分配不同的值。

我做了一些搜索,但不确定如何进行。

我的 circle.java 类的代码:

public class Circle {

    // intialize variable radius
    private double radius;
    // create constant PI
    public final static double PI = 3.14159;

    // Circle constructtor
    public Circle(double r) {
        radius = r;
    }

    // setRadius method
    public void setRadius(double r) {
        radius = r;
    }

    // getRaduis method
    public double getRadius() {
        return radius;
    }

    // getArea method
    public double getArea() {
        double area = PI * radius * radius;
        return area;
    }

    // getDiameter method
    public double getDiameter() {
        double diameter = 2 * radius;
        return diameter;
    }

    // getCircumference method
    public double getCircumference() {
        double circumference = 2 * PI * radius;
        return circumference;
    }

}

我的程序 circleTest 的代码:

package circle;

import java.util.Scanner;
public class CircleTest {

    public static void main(String[] args) {
        double circleRadius;
        double radius;

        // import scanner
        Scanner keyboard = new Scanner(System.in);

        // ask user for raduis and store in a local variable
        System.out.print("What is the radius of the circle?");
        circleRadius = keyboard.nextDouble();

        // Create a circle object called circle1
        Circle circle1 = new Circle(circleRadius);

        // display area using getArea method for circle1
        System.out.println("The area of the circle is " + circle1.getArea());

        // display diameter for circle1 using getDiameter method
        System.out.println("The diamter of a the circle is "
                + circle1.getDiameter());

        // display the circumference of circle1 using getCircumference
        System.out.println("The circumference of a circle is "
                + circle1.getCircumference());

        // ask user for radius and store in a local variable
        System.out.print("What is the radius of the circle?");
        circleRadius = keyboard.nextDouble();

    }
}
4

0 回答 0