【问题标题】:Struggling with Greenfoot与绿脚斗争
【发布时间】:2015-01-18 12:23:53
【问题描述】:

在我的 Uni 模块的作业中,我被分配了题为问题解决和编程的作业。

给了我一个包含错误的场景,通过阅读作业,下面列出的代码就是错误所在。

到目前为止,我发现在我的代码的 public void key 部分中,我不断收到类预期错误,但是由于我是一个完全的编程新手,我不知道如何解决这个问题。

我试图在互联网上找到解决方案,但是我不知道要搜索什么,尽管我的朋友说如果您遇到与编程相关的问题,使用 stackoverflow 非常好,所以我想我会尝试一下感谢一些帮助。

public boolean canMove(int x, int y) {

    Actor sand;
    sand=getOneObjectAtOffset(x,y,sandroad.class);

    //the section below checks if there is a block you can move to
    // if there is it sets sand to a vlaue otherwise it says null
    // The errors are in this section
    boolean flag=true;
    if (sand !=null)
    {
        flag=false;
    }
    return flag;
}
public void key()
{
   //Note 1: Down the page increase the y value and going to the right increases the x value
   //Note 2: Each block is 60 pixels wide and high 
    int leftChange=//choose the appropriate left step size ; 
    int rightChange=//choose the appropriate right step size ; 
    int upChange=//choose the appropriate up step size ; 
    int downChange=//choose the appropriate down step size ; 
    if (Greenfoot.isKeyDown("left"))
    {
        if (canMove(leftChange, 0)==true)
        setLocation(getX()+leftChange, getY()) ;
    }
    if (Greenfoot.isKeyDown("right"))
    {
       if (canMove(rightChange, 0)==true)
        setLocation(getX()+rightChange, getY()) ; 
    }
    if (Greenfoot.isKeyDown("up"))
    {
        if (canMove(0, upChange)==true)
        setLocation(getX(), getY()+upChange) ;
    }
    if (Greenfoot.isKeyDown("down"))
    {
        if (canMove(0, downChange)==true)
        setLocation(getX(), getY()+downChange) ;
    }
}

【问题讨论】:

  • 这些方法是某个类的一部分吗?
  • 实际上阅读了我的所有代码后,我认为 public void 关键部分下的方法没有类,因为所有其他部分(例如 public void win 和 public boolean canMove)在其中有一个 .class公钥当然不是我不确定你具体说的是哪种方法。
  • canMovekey。 Java 中的任何方法都必须是某个类的一部分。

标签: java greenfoot


【解决方案1】:

key() 中的多个 int's 没有设置为任何值。你不能就这样离开他们。

    int leftChange=//choose the appropriate left step size ; 
    int rightChange=//choose the appropriate right step size ; 
    int upChange=//choose the appropriate up step size ; 
    int downChange=//choose the appropriate down step size ; 

应该是这样的

int leftChange=4;

每一个。

【讨论】:

  • 好吧,解决了我的问题,不敢相信这么简单,谢谢你的帮助^_^
猜你喜欢
  • 2020-09-18
  • 2018-05-23
  • 2013-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-30
  • 2014-10-04
相关资源
最近更新 更多