0

每当我点击编译时,它都会说无论 private 所在的位置都是非法的表达式开始。这是代码:

    /**
     * Act - do whatever the PlatformJumper wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if(inTheAir)
        {
            fall();
        } else {
            getCommand();
        }
        move();

        if(isTouching(snowball.class))
        {
            get (snowball.class);
        }    

    private void run (String direction)
    {
        if(direction=="left")
            deltaX = walkSpeed*-1;
        else
            deltaX = walkSpeed;
    }

    private void stop ()
    {
        deltaX = 0;
    }

    private void jump()
    {
        deltaY += jumpHeight;
        inTheAir = true;
    }

    /*
     * fall() will be called whenever BallGuy is in the air. Decreases the deltaY by 1, creating
     * gravity.
     */
    private void fall()
    {
        deltaY-=fallSpeed;
    }

    private void move()
    {
        double newX = getX() + deltaX;
        double newY = getY() - deltaY;

        Actor platformBelow = getOneObjectAtOffset(0, groundHeight + 5, Platform.class);
        Actor platformAbove = getOneObjectAtOffset(0, -(groundHeight + 5), Platform.class);
        Actor platformToRight = getOneObjectAtOffset(sideWidth+5, 0, Platform.class);
        Actor platformToLeft = getOneObjectAtOffset(-(sideWidth+5), 0, Platform.class);
        if(platformBelow!=null)
        {
            if(deltaY<0)
            {
                deltaY = 0;
                inTheAir = false;
                GreenfootImage platformImage = platformBelow.getImage();
                int topOfPlatform = platformBelow.getY() - platformImage.getHeight()/2;
                newY = topOfPlatform - groundHeight;
            }
        }else if(getY() >= worldHeight - groundHeight) {
            if(deltaY < 0)
            {
                deltaY = 0;
                inTheAir = false;
                newY = worldHeight - groundHeight;
            }
        } else {
            inTheAir = true;
        }
        if(platformAbove != null)
        {
            if(deltaY>0)
            {
                deltaY=0;

                GreenfootImage platformImage = platformAbove.getImage();
                int bottomOfPlatform = platformAbove.getY() + platformImage.getHeight()/2;
                newY = bottomOfPlatform + groundHeight;
            }
        }
        if(getX()<=sideWidth)
        {
            deltaX = Math.abs(deltaX);
        }
        if(getX()>=worldWidth-sideWidth)
        {
            deltaX = Math.abs(deltaX) * -1;
        }
        if(platformToRight!=null)
        {
            deltaX = Math.abs(deltaX) * -1;
        }
        if(platformToLeft!=null)
        {
            deltaX = Math.abs(deltaX);
        }
        setLocation((int)newX,(int)newY);
    }

    private void getCommand()
    {
        if(Greenfoot.isKeyDown("left"))
        {
            run("left");
        } else if (Greenfoot.isKeyDown("right"))
        {
            run("right");
        } else 
        {
            stop();
        }

        if(Greenfoot.isKeyDown("up"))
        {
            jump();
        }
    }
}
4

1 回答 1

0
import greenfoot.*;

/**
 * Write a description of class BallGuy here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class BallGuy extends
{
    public BallGuy()
    {
    }

在代码的顶部应该有类似这样的内容。现在编译器认为代码底部有额外的括号。我假设你在这个问题上忽略了这一点。

还:

在第一个私有方法上方的行上添加一个括号。act() 方法未关闭。

public void act() 
    {
        if(inTheAir)
        {
            fall();
        } else {
            getCommand();
        }
        move();

        if(isTouching(snowball.class))
        {
            get (snowball.class);
        }    
    }

private void run (String direction)
{
    if(direction=="left")
        deltaX = walkSpeed*-1;
    else
        deltaX = walkSpeed;
}

像那样。

于 2015-10-01T21:31:55.367 回答