import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot)
/**
* This class defines a crab. Crabs live on the beach. They like sand worms
* (very yummy, especially the green ones).
*
* Version: 4
*
* The crab is keyboard controlled and eats worms. In this version, we have added
* a sound when the crab eats a worm.
*/
public class Crab extends Actor
{
private GreenfootImage image1;
private GreenfootImage image2;
private int age;
/**
* Create a crab and initialize its two images.
*/
public Crab()
{
image1 = new GreenfootImage("crab.png");
image2 = new GreenfootImage("crab2.png");
setImage(image1);
}
/**
* Act - do whatever the crab wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
boolean isAlive;
int n;
checkKeypress();
move(5);
lookForWorm();
if ( getImage() == image1)
{setImage(image2);
}
else
{
setImage(image1);
}
}
/**
* Check whether a control key on the keyboard has been pressed.
* If it has, react accordingly.
*/
public void checkKeypress()
{
if (Greenfoot.isKeyDown("up"))
{
setLocation(getX(), getY()-4);
}
if (Greenfoot.isKeyDown("down"))
{
setLocation(getX(), getY()+4);
}
}
/**
* Check whether we have stumbled upon a worm.
* If we have, eat it. If not, do nothing.
*/
public void lookForWorm()
{
if ( isTouching(Worm.class) )
{
removeTouching(Worm.class);
Greenfoot.playSound("slurp.wav");
wormsEaten = wormsEaten + 1;
if (wormsEaten == 8)
{
Greenfoot.playSound("fanfare.wav");
Greenfoot.stop();
}
}
}
答案 0 :(得分:1)
这个编译器错误(对于指定它不是运行时错误很重要)几乎总是意味着你在某个地方错过了一个结束大括号 - 在这种情况下,它看起来你错过了括号来关闭类(在源的底部。)
如果你想要一个更友好的错误转录,你可以把它想象成编译器说“我在关闭所有范围块之前到达了文件的末尾。”