當我運行它時,我的代碼返回一個錯誤,告訴我「有太多的{字符沒有}來匹配它」,但我已經檢查並重新檢查,重新檢查,甚至有別人檢查我,無濟於事。在處理中加上大括號時出現問題
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) {
我在另一臺機器上重新創建了錯誤,但看到了教程視頻中使用的代碼沒有任何問題。任何和所有的幫助將不勝感激:)
您使用的IDE像Eclipse ?? ..如果是的話,用ctrl + a和Ctrl + Shift + F格式化代碼..否則,你也可以使用Notepad ++ ..避免在普通的txt文件中編寫代碼......未格式化的代碼將很難閱讀.. – TheLostMind
我在Processing編輯器中編寫,定期自動格式化任何我想念的東西與CTRL + T :) – BenjiHare
我知道這個問題已經解決,但是您可以使用PDE X(我認爲這就是它的名字)給你的代碼完成和語法檢查。它仍然處於測試階段,但它將成爲下一個處理IDE又名PDE:P這是一些信息:http://www.mkmoharana.com/2013/09/announcing-pde-x.html –