[英]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 方法时做错了。
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.'a'
, "hello world"
, 3
, 4.0f
or constants that are defined with the static
keyword. 'a'
、 "hello world"
、 3
、 4.0f
或用static
关键字定义的常量。 (When you want to use non-static values use if) 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.