代码之家  ›  专栏  ›  技术社区  ›  BenjiHare

处理过程中出现大括号问题

  •  0
  • BenjiHare  · 技术社区  · 10 年前

    当我运行代码时,我的代码返回一个错误,告诉我“有太多的{个字符没有一个}与之匹配”,但我已经检查了一遍,再次检查了三遍,甚至让其他人为我检查,但都无济于事。

    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) {
    

    我在另一台机器上重新创建了该错误,但在教程视频中看到的代码没有任何问题。非常感谢您的任何帮助:)

    4 回复  |  直到 10 年前
        1
  •  3
  •   user2424380    10 年前

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

        2
  •  2
  •   Hüseyin BABAL    10 年前

    count 变量是局部变量,但您在函数之外使用了它。 steer acc 同样如此。更新如下:;

    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
  •  1
  •   Donald W. Smith    10 年前

    您的缩进做得很好,所以大括号的位置是正确的(您可能使用了“编辑”、“自动套用格式”)。我向我的编程学生建议,他们在“结束”大括号中添加注释,以防止出现此类问题:

    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
  •  0
  •   jason    10 年前

    如果您使用IDEA,您可以很容易地找到编译错误,如果(count>0)不在某个方法中,似乎您在该语句之前错误地添加了一个“}”。