简体   繁体   English

创建一个类并使用 awt.point

[英]Making a class and using awt.point

I am trying to write a class Robot that simulates a robot wandering on an infinite plane.我正在尝试编写一个类 Robot 来模拟在无限平面上徘徊的机器人。 The robot is located at a point with integer coordinates and faces north, east, south, or west.机器人位于具有整数坐标且面向北、东、南或西的点。 I need to supply methods public void turnLeft(), public void turnRight(), public void move(), public Point getLocation(), public String getDirection() Here is what I have:我需要提供方法 public void turnLeft()、public void turnRight()、public void move()、public Point getLocation()、public String getDirection() 这是我所拥有的:

import java.awt.Point;

public class Robot 
{
public static void main (String [] args){

    Point location;
    char direction;


    public Robot()
    {
        location = new Point();
     char direction = 'N';
    }

    public void turnLeft()
    {

       char direction = 'S';
    }

    public void turnRight()
    {
       char direction = 'E';
    }

    public void move()
    {
        switch(direction)
        {
            case turnLeft():
            {

                break;
            }

            case turnRight():
            {
                break;
            }
        }
    }

    public Point getLocation()
    {
        return location;
    }

    public String getDirection()
    {
        return direction;
    }
}
}

However I know I'm doing something wrong with the move method .但是我知道我在使用 move 方法时做错了。

  1. The methods turnLeft() and turnRight() have a return type of void (which means it does not return anything) so you definitely can't use them in your switch-case-expression.方法turnLeft()turnRight()的返回类型为void (这意味着它不返回任何内容),因此您绝对不能在 switch- turnRight()表达式中使用它们。 You have to put a value behind a case and a void is definitely none.您必须在案例后面放置一个值,而 void 绝对没有。
  2. Case only accepts static values like 'a' , "hello world" , 3 , 4.0f or constants that are defined with the static keyword. Case 只接受静态值,如'a'"hello world"34.0f或用static关键字定义的常量。 (When you want to use non-static values use if) (当您想使用非静态值时,请使用 if)
  3. As far as I can see you defined the constructor, class members and methods that belong inside class body inside the main method.据我所知,您在 main 方法中定义了属于类体内的构造函数、类成员和方法。

And: Think about what you want to do.以及:想想你想做什么。 Do you really want to check for the result of a function or do you want to check for every possible direction ( 'N' , 'S' , 'E' , 'W' )?您真的要检查函数的结果还是要检查每个可能的方向( 'N''S''E''W' )? If so you have to to switch over them.如果是这样,您必须切换它们。 (I also recommend defining them like public static char N='N' etc.; this will prevent you from typing an invalid direction because your IDE will point out the mistake. Even better is using an enum ) (我还建议将它们定义为public static char N='N'等;这将防止您输入无效的方向,因为您的 IDE 会指出错误。更好的是使用enum

I don't want to sound rude but You seem like you don't know a lot about java so I recommend you to actually learn at least some basics of this language.我不想听起来很粗鲁,但您似乎对 Java 了解不多,因此我建议您至少实际学习该语言的一些基础知识。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM