如何摆脱我的代码中的“找不到符号”错误?

我目前正在做一个项目,并且不断收到错误消息。我被困住了,联系了很多人(包括我的导师),现在我求助于你们。


到目前为止,这是我的代码。


public class Circle


    private int radius = getRadius();

    private double area = getArea();


    public Circle(int r) 

    {

        r = radius;

    }


    public int getRadius()

    {

        return radius;

    }


    public double getArea(int r)

    {

        return area = Math.PI * r * r;  

    }

}

/


java.util.Scanner;


public class CircleTest

{

    public CircleTest()

    {

        int radius = getRadius();

        double area = getArea(r);       

    }   


    public static void main (String[] args) 

    {


        Scanner kboard = new Scanner(System.in);


        System.out.print("Give the radius of a circle. ");

        String area = kboard.nextLine();


        System.out.println("The area of the circle is... " + 

area);

        System.out.println();


        kboard.close();

    }

}

C:\Users\jthom\My Work\Circle\src\CircleTest.java:18:错误:找不到符号


双面积 = getArea(r);


符号:变量 r


位置:类 CircleTest


.


C:\Users\jthom\My Work\Circle\src\CircleTest.java:33:错误:找不到符号


System.out.println("圆的面积是……" + area);


符号:变量区


位置:类 CircleTest


2 个错误


紫衣仙女
浏览 179回答 2
2回答

qq_遁去的一_1

让我们从类 Circle 开始。这是您的原始代码:public class Circle&nbsp; &nbsp; private int radius = getRadius();&nbsp; &nbsp; private double area = getArea();&nbsp; &nbsp; public Circle(int r)&nbsp;&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; r = radius;&nbsp; &nbsp; }&nbsp; &nbsp; public int getRadius()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return radius;&nbsp; &nbsp; }&nbsp; &nbsp; public double getArea(int r)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return area = Math.PI * r * r;&nbsp;&nbsp;&nbsp; &nbsp; }}对于您的变量,您不需要“面积”,因为它是计算出来的。此外,除了在构造函数中,您不应该将半径分配给任何东西:&nbsp; &nbsp; private int radius;&nbsp; &nbsp; //private double area = getArea(); <-- don't need this variable at all您将“r”传递给构造函数,但随后错误地尝试为其分配“半径”值。这是倒退;您应该将“r”值分配给“radius”:&nbsp; &nbsp; public Circle(int r)&nbsp;&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; radius = r;&nbsp; &nbsp; }最后,在 中getArea(),您不需要传入的半径或“区域”;只需返回计算值(使用“半径”中的存储值,而不是“r”):&nbsp; &nbsp; public double getArea()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return Math.PI * radius * radius;&nbsp;&nbsp;&nbsp; &nbsp; }总而言之,您的 Circle 类应该看起来更像:public class Circle&nbsp; &nbsp; private int radius;&nbsp; &nbsp; public Circle(int r)&nbsp;&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; radius = r;&nbsp; &nbsp; }&nbsp; &nbsp; public int getRadius()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return radius;&nbsp; &nbsp; }&nbsp; &nbsp; public double getArea()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return Math.PI * radius * radius;&nbsp;&nbsp;&nbsp; &nbsp; }}在 CircleTest 中,您应该首先从用户那里获取半径,然后将其传递给 Circle 的构造函数。最后,使用您的 Circle 实例,调用它的 getRadius() 和 getArea() 方法。

慕桂英3389331

你还没有定义变量r和area,来帮助解决这个问题替换double area = getArea(r);为double area = circle.getArea(radius);和System.out.println("The area of the circle is... " + area);和System.out.println("The area of the circle is... " + circle.getArea(Integer.parseInt(area)));这里的圆是类圆的对象Circle&nbsp;circle&nbsp;=&nbsp;new&nbsp;Circle(radius)&nbsp;//Note:&nbsp;circle&nbsp;cannot&nbsp;be&nbsp;accessed&nbsp;from&nbsp;main&nbsp;function&nbsp;create&nbsp;another&nbsp;object&nbsp;for&nbsp;class&nbsp;circle&nbsp;again&nbsp;in&nbsp;main它应该有帮助
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java