2014-02-20 61 views
0

當我運行它時,我的代碼返回一個錯誤,告訴我「有太多的{字符沒有}來匹配它」,但我已經檢查並重新檢查,重新檢查,甚至有別人檢查我,無濟於事。在處理中加上大括號時出現問題

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

我在另一臺機器上重新創建了錯誤,但看到了教程視頻中使用的代碼沒有任何問題。任何和所有的幫助將不勝感激:)

+1

您使用的IDE像Eclipse ?? ..如果是的話,用ctrl + a和Ctrl + Shift + F格式化代碼..否則,你也可以使用Notepad ++ ..避免在普通的txt文件中編寫代碼......未格式化的代碼將很難閱讀.. – TheLostMind

+0

我在Processing編輯器中編寫,定期自動格式化任何我想念的東西與CTRL + T :) – BenjiHare

+0

我知道這個問題已經解決,但是您可以使用PDE X(我認爲這就是它的名字)給你的代碼完成和語法檢查。它仍然處於測試階段,但它將成爲下一個處理IDE又名PDE:P這是一些信息:http://www.mkmoharana.com/2013/09/announcing-pde-x.html –

回答

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); 
    } 
+0

好眼睛!謝謝:) – BenjiHare

3

我認爲你的問題是在103行,還有一個}。 if(count> 0)行在方法之外。

+0

正確的你!非常感謝! – BenjiHare

0

如果你使用一個想法,你可以很容易找到的編譯錯誤,如果(計數> 0),超出的方法,看來你錯此語句前加上一個「}」。

+0

你是對的!非常感謝:) – BenjiHare

1

你已經完成了很好的縮進,所以你的大括號的位置是正確的(你可能使用過編輯,自動格式)。我建議我的編程的學生,他們發表評論「結束」大括號,以防止此類問題:

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 
+0

這是一個非常有用的想法!我一定會試着強迫自己實現它:) – BenjiHare