【问题标题】:Having an issue with curly brackets in Processing处理中的大括号有问题
【发布时间】:2014-02-20 09:16:41
【问题描述】:

我的代码在运行时返回一个错误,告诉我“有太多没有 } 的 { 字符来匹配它”,但我已经检查并重新检查和三次重新检查,甚至让其他人检查我,无济于事。

class Ball {
  //Global Vars
  //float x=0;
  //float y=0;
  //float speedx = random(-5,5);
  //float speedy = random(-1,1);

  Vec3D loc = new Vec3D (0, 0, 0);
  Vec3D speed = new Vec3D (random(-4, 4), random(-1, 1), 0);

  Vec3D acc = new Vec3D();

  Vec3D grav = new Vec3D (0, random(0.05, 0.25), 0);

  //construct
  Ball(Vec3D _loc) {

    loc = _loc;
  }

  //functions
  void run() {
    display();
    move();
    bounce();
    //  gravity();
    lineBetween();
    flock();
  }

  void display() {
    stroke(0);
    ellipse(loc.x, loc.y, 20, 20);
  }


  void move() {
    // x += speedx;
    // y += speedy;

    speed.addSelf(acc);
    speed.limit(6);
    loc.addSelf(speed);
    acc.clear();
  }

  void bounce() {
    if (loc.x > width) {
      speed.x = speed.x*-1;
    }
    if (loc.x < width-width) {
      speed.x = speed.x*-1;
    }
    if (loc.y > height) {
      speed.y = speed.y*-1;
    }
    if (loc.y < height-height) {
      speed.y = speed.y*-1;
    }
  }

  void gravity() {
    //speedy += 0.15;

    speed.addSelf(grav);
  }

  void lineBetween() {
    //ballCollection
    for (int i=0; i<ballCollection.size();i++) {
      Ball other = (Ball) ballCollection.get(i);
      float distance = loc.distanceTo(other.loc);
      if (distance > 0 && distance < 80) {
        stroke(255, 0, 255);
        strokeWeight(0.2);
        line(loc.x, loc.y, other.loc.x, other.loc.y);
      }
    }
  }

  void flock() {
    separate();
    // cohesion();
    // align();
  }

  void separate(float magnitude) {
    Vec3D steer = new Vec3D();
    int count = 0;

    for (int i=0; i<ballCollection.size();i++) {
      Ball other = (Ball) ballCollection.get(i);
      float distance = loc.distanceTo(other.loc);
      if (distance > 0 && distance < 40) {

        Vec3D diff = loc.sub(other.loc);
        diff.normalizeTo(1.0/distance);

        steer.addSelf(diff);
         count++;
      }
    }
  }

  if (count>0) {
    steer.scaleSelf(1.0/count);
  }

  steer.scaleSelf(magnitude);
  acc.addSelf(steer);
}

错误信息突出显示第 106 行;

if (count>0) {

我在另一台机器上重新创建了错误,但看到教程视频中使用的代码没有任何问题。任何和所有的帮助将不胜感激:)

【问题讨论】:

  • 你在使用像eclipse这样的IDE吗??..如果是,那么使用ctrl+a和ctrl+shift+f来格式化代码..否则你也可以使用Notepad++..避免编写代码在普通的 txt 文件中......未格式化的代码将难以阅读......
  • 我正在处理编辑器中编写,定期使用 CTRL+T 自动格式化我错过的任何内容 :)
  • 我知道这已经解决了,但是您可以使用 PDE X(我认为这就是它的名称)为您提供动态代码完成和语法检查。它仍处于测试阶段,但将成为下一个处理 IDE,即 PDE:P 以下是一些信息:mkmoharana.com/2013/09/announcing-pde-x.html

标签: java processing curly-braces curly-brackets


【解决方案1】:

我认为你的问题在第 103 行,有一个额外的 }。 if(count>0) 行在方法之外。

【讨论】:

    【解决方案2】:

    count 变量是局部变量,但是您在函数之外使用了它。 steeracc 也一样。如下更新;

    void separate(float magnitude) {
        Vec3D steer = new Vec3D();
        int count = 0;
    
        for (int i=0; i<ballCollection.size();i++) {
          Ball other = (Ball) ballCollection.get(i);
          float distance = loc.distanceTo(other.loc);
          if (distance > 0 && distance < 40) {
    
            Vec3D diff = loc.sub(other.loc);
            diff.normalizeTo(1.0/distance);
    
            steer.addSelf(diff);
             count++;
          }
        }
    
        if (count>0) {
            steer.scaleSelf(1.0/count);
        }
    
          steer.scaleSelf(magnitude);
          acc.addSelf(steer);
      }
    

    【讨论】:

      【解决方案3】:

      你的缩进做得很好,所以大括号的位置是正确的(你可能使用了编辑,自动格式化)。我建议我的编程学生评论“结束”花括号以防止此类问题:

      void separate(float magnitude) {
        Vec3D steer = new Vec3D();
        int count = 0;
      
        for (int i=0; i<ballCollection.size();i++) {
          Ball other = (Ball) ballCollection.get(i);
          float distance = loc.distanceTo(other.loc);
          if (distance > 0 && distance < 40) {
      
            Vec3D diff = loc.sub(other.loc);
            diff.normalizeTo(1.0/distance);
      
            steer.addSelf(diff);
            count++;
          }  // end if distance
        }  // end for
      }  // end separate method
      

      【讨论】:

      • 这是一个非常有用的想法!我一定会尝试强迫自己去实现它:)
      【解决方案4】:

      如果你使用IDEA,很容易发现编译错误,if(count>0)是out of a method,好像是你错误的在这个语句前加了一个“}”。

      【讨论】:

        猜你喜欢
        • 2019-06-28
        • 2013-10-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-07
        • 1970-01-01
        相关资源
        最近更新 更多