2014-02-20 3 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를 사용 : 나는 그들이 이러한 문제를 방지하기 위해 '말'중괄호를 언급 나의 프로그래밍 학생들에게 제안 코드를 포맷하는 것 .. 그렇지 않으면 메모장 ++을 사용할 수도 있습니다. 일반 txt 파일에 코드를 작성하는 것을 피하십시오 ... 포맷되지 않은 코드는 읽기가 어려울 것입니다 .. – TheLostMind

+0

저는 처리 편집기에서 정기적으로 자동 서식을 지정합니다. CTRL + T : – BenjiHare

+0

이것이 해결되었다는 것을 알았지 만, PDE X (나는 그것이 무엇이라고 불렀는지)를 사용하여 플라이 코드 완성과 구문 검사를 할 수 있습니다. 아직 베타 버전이지만 다음 프로세싱 IDE 일명 PDE : P가 될 것입니다. 여기에 몇 가지 정보가 있습니다 : http://www.mkmoharana.com/2013/09/announcing-pde-x.html –

답변

2

count 변수는 지역 변수이지만 함수 밖에서 사용했습니다. steeracc도 동일합니다. 아래처럼 업데이트하십시오. (> 0 카운트는) 방법을 벗어나면 당신은 IDEA를 사용하는 경우

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

당신이 맞습니다! 고마워요 :) – BenjiHare

1

들여 쓰기가 잘되어있어 중괄호의 위치가 정확합니다 (편집, 자동 서식 사용). 예, 다음 Ctrl + A와 Ctrl + Shift + F를 사용하는 경우

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