【问题标题】:I keep receiving an error "reached end of file while parsing"我不断收到错误“解析时到达文件末尾”
【发布时间】:2017-02-23 01:59:21
【问题描述】:
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();
            }
        }
    }

【问题讨论】:

  • 解析什么?也许“slurp.wav: 和”fanfare.wav“要么找不到要么无效。
  • 因为您在class 末尾缺少}

标签: java


【解决方案1】:

这个 compiler 错误(重要的是要指定它不是 runtime 错误)几乎总是意味着您在某处缺少一个右花括号 - 在这种情况下,它看起来你错过了关闭课程的大括号(在源代码的底部。)

如果您希望对错误进行更友好的转录,可以将其视为编译器说“我在所有范围块关闭之前到达了文件末尾。”

【讨论】:

    猜你喜欢
    • 2017-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多