【问题标题】:Trying to call a function in java, complains ".class expected".试图在 java 中调用一个函数,抱怨“.class expected”。
【发布时间】:2012-02-07 16:35:22
【问题描述】:

这里是新手程序员。尝试学习静态方法和递归。不知道为什么每当我尝试调用“drawCircle()”时,我总是收到“.class expected”错误。我的代码如下。请帮忙?谢谢!

public class Drawliin
{
    public static void drawCircle(int numberOfTimes, double radius, double center[])
    {
        int rep = 1;
        if (rep == 1)
        {
            StdDraw.circle(center[0], center[1], radius);
            rep++;
        }
        else if (rep <= numberOfTimes)
            {
            StdDraw.circle(center[0 + radius], center[1], radius);
            StdDraw.circle(center[0 - radius], center[1], radius);
            StdDraw.circle(center[0], center[1 + radius], radius);
            StdDraw.circle(center[0], center[1 - radius], radius);
            rep++;
            drawCircle(numberOfTimes, radius, center[]);
            }
        }

    public static void main(String[] args)
    {
        int N = Integer.parseInt(args[0]);
        double r = Double.parseDouble(args[1]);
        StdDraw.setXscale(-10, 10);
        StdDraw.setYscale(-10, 10);
        double c[] = new double[2];
        drawCircle(N, r, c[]);
    }
}

【问题讨论】:

    标签: java class function call


    【解决方案1】:

    应该是:

    drawCircle(N, r, c);
    

    您只需传递c。您无需再次指出它是一个数组。

    【讨论】:

      【解决方案2】:

      您的问题出在以下几行:

      drawCircle(N, r, c[]);
      drawCircle(numberOfTimes, radius, center[]);
      

      他们应该是:

      drawCircle(N, r, c);
      drawCircle(numberOfTimes, radius, center);
      

      您不需要再次将其定义为数组,您已在参数中进行了定义。只需将参数传递给函数。

      【讨论】:

        【解决方案3】:

        去掉'center'和'c:中的大括号

        drawCircle(numberOfTimes, radius, center);
        
        drawCircle(N, r, c);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2022-01-23
          • 1970-01-01
          • 2013-03-30
          • 1970-01-01
          • 2013-03-22
          • 2011-11-29
          • 1970-01-01
          • 2019-02-13
          相关资源
          最近更新 更多