将 java 方法合并到一个主方法中时遇到问题

发布于 2024-11-17 07:29:50 字数 1104 浏览 4 评论 0原文

class Bug {

// An ant is represented by the coordinates of its location,
// and the direction it is facing.
Integer x;
Integer y;
Dir dir;

enum Dir { E,W,N,S }
}

Bug(Integer x, Integer y, Dir dir) {
    this.x = x;
    this.y = y;
    this.dir = dir;
}}

class BugWorld {


Integer theBreadth, theHeight;
Board board;
Bug bug;

BugWorld(Integer breadth, Integer height) {
    board = new board(breadth, height);
    bug = new Bug(breadth/2, height/2, Ant.Direction.Y);
    theBreadth = breadth;
    theHeight = height;
}

我已经有了以下内容:

Status status(Integer x, Integer y) {
    return board[x][y];
}


void update(Integer x, Integer y) {
    board[x][y].next();
}

下面的这部分是我遇到一些麻烦的地方:

/* Take the world of the bug to the next step. */
void step() {
    // 1) Get the state at the present bug position.
          //I've done the following (next line) so far.
           Bug status(Integer x, Integer y); ...?

    // 2) Change the 'status' at that position.
             .............?
}

它只是将我遇到问题的这些内容组合起来。

class Bug {

// An ant is represented by the coordinates of its location,
// and the direction it is facing.
Integer x;
Integer y;
Dir dir;

enum Dir { E,W,N,S }
}

Bug(Integer x, Integer y, Dir dir) {
    this.x = x;
    this.y = y;
    this.dir = dir;
}}

class BugWorld {


Integer theBreadth, theHeight;
Board board;
Bug bug;

BugWorld(Integer breadth, Integer height) {
    board = new board(breadth, height);
    bug = new Bug(breadth/2, height/2, Ant.Direction.Y);
    theBreadth = breadth;
    theHeight = height;
}

I already have the following:

Status status(Integer x, Integer y) {
    return board[x][y];
}


void update(Integer x, Integer y) {
    board[x][y].next();
}

This part below is where I'm having some trouble:

/* Take the world of the bug to the next step. */
void step() {
    // 1) Get the state at the present bug position.
          //I've done the following (next line) so far.
           Bug status(Integer x, Integer y); ...?

    // 2) Change the 'status' at that position.
             .............?
}

It's just combining these that I'm having trouble with.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

温暖的光 2024-11-24 07:29:50

很难理解你想要做什么,但我认为这就是你想要的

void step(){

     //get the location of the bug 
     int x = bug.x;
     int y = bug.y;

     //gets the status of the location of the bug
     Status s = status(x,y);

     //move the bug, or do something based on the status of the place

     //update the status of the location the bug was originally on
     update(x,y)
}

It's hard to understand what you're trying to do, but I think here's what you want

void step(){

     //get the location of the bug 
     int x = bug.x;
     int y = bug.y;

     //gets the status of the location of the bug
     Status s = status(x,y);

     //move the bug, or do something based on the status of the place

     //update the status of the location the bug was originally on
     update(x,y)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文