You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用java.awt.Point编写无限平面漫游Robot类,move方法问题求助

我来帮你搞定这个Robot类的问题!首先咱们得把方向逻辑和移动逻辑理清楚,然后修正move方法的问题。下面是完整的可运行实现,我会一步步拆解关键部分:

import java.awt.Point;

public class Robot {
    // 用枚举定义四个方向,比直接用字符串更安全规范,避免拼写错误
    private enum Direction {
        NORTH, EAST, SOUTH, WEST
    }

    private Point location;
    private Direction currentDirection;

    // 默认构造:初始位置在(0,0),默认朝北
    public Robot() {
        this.location = new Point(0, 0);
        this.currentDirection = Direction.NORTH;
    }

    // 自定义初始位置和方向的构造方法(可选,方便扩展)
    public Robot(int x, int y, String initialDirection) {
        this.location = new Point(x, y);
        this.currentDirection = Direction.valueOf(initialDirection.toUpperCase());
    }

    public void turnLeft() {
        // 左转逻辑:NORTH → WEST → SOUTH → EAST → NORTH
        switch(currentDirection) {
            case NORTH: currentDirection = Direction.WEST; break;
            case WEST: currentDirection = Direction.SOUTH; break;
            case SOUTH: currentDirection = Direction.EAST; break;
            case EAST: currentDirection = Direction.NORTH; break;
        }
        // 嫌switch麻烦?可以用数组索引简化:
        // Direction[] dirs = Direction.values();
        // currentDirection = dirs[(currentDirection.ordinal() + 3) % 4];
    }

    public void turnRight() {
        // 右转逻辑:NORTH → EAST → SOUTH → WEST → NORTH
        switch(currentDirection) {
            case NORTH: currentDirection = Direction.EAST; break;
            case EAST: currentDirection = Direction.SOUTH; break;
            case SOUTH: currentDirection = Direction.WEST; break;
            case WEST: currentDirection = Direction.NORTH; break;
        }
        // 同样可以用数组索引简化:
        // Direction[] dirs = Direction.values();
        // currentDirection = dirs[(currentDirection.ordinal() + 1) % 4];
    }

    public void move() {
        // 这里是你之前出错的核心!方向对应的坐标修改要匹配平面逻辑:
        switch(currentDirection) {
            case NORTH: location.y += 1; break;  // 朝北走,y轴正方向
            case EAST: location.x += 1; break;   // 朝东走,x轴正方向
            case SOUTH: location.y -= 1; break;  // 朝南走,y轴负方向
            case WEST: location.x -= 1; break;   // 朝西走,x轴负方向
        }
    }

    public Point getLocation() {
        // 返回新的Point对象,避免外部直接修改机器人的位置(封装性)
        return new Point(location);
    }

    public String getDirection() {
        // 返回方向的字符串形式,如需小写可以加.toLowerCase()
        return currentDirection.name();
    }

    // 测试用例,验证功能是否正常
    public static void main(String[] args) {
        Robot robot = new Robot();
        System.out.println("初始状态:" + robot.getLocation() + ",朝向" + robot.getDirection());
        
        robot.move();
        System.out.println("朝北走一步:" + robot.getLocation());
        
        robot.turnRight();
        robot.move();
        System.out.println("右转朝东走一步:" + robot.getLocation());
        
        robot.turnLeft();
        robot.move();
        System.out.println("左转朝北走一步:" + robot.getLocation());
    }
}

关键细节说明:

  1. 方向的安全管理:用枚举Direction替代字符串,能避免诸如把"north"写成"noth"这类低级错误,编译器会帮你做合法性检查。
  2. 移动逻辑的修正:你之前的move方法大概率是方向和坐标修改不匹配,比如把朝南走写成了y +=1(实际应该是y -=1),或者朝西走误改了y坐标。上面的代码明确对应了每个方向的坐标变化规则。
  3. 封装性保障:getLocation返回新的Point对象,而不是直接返回成员变量,这样外部代码不能偷偷修改机器人的位置,符合面向对象的封装原则。
  4. 两种转向实现:提供了switch和数组索引两种转向逻辑,你可以根据自己的习惯选择,数组索引的方式更简洁,适合喜欢精简代码的场景。

内容的提问来源于stack exchange,提问作者missmadoka

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 10:05:59