Java 编写了一个具有两个int参数的方法。如何使用下面提供的信息声明更多的局部int变量
我编写了一个公共实例方法moveBy(),它的第一个参数是一个int,表示移动的距离,第二个参数是一个char,表示移动的方向。它不应该返回任何值 表1:方向和相应的x和y增量 Direction xInc yInc (this is not a code) Right 'R' 1 0 Left 'L' -1 0 Up 'U' 0 -1 Down 'D' 0 1 或者我在第一个位置尝试了这个,但它显示“预期标识符”Java 编写了一个具有两个int参数的方法。如何使用下面提供的信息声明更多的局部int变量,java,int,bluej,local-variables,Java,Int,Bluej,Local Variables,我编写了一个公共实例方法moveBy(),它的第一个参数是一个int,表示移动的距离,第二个参数是一个char,表示移动的方向。它不应该返回任何值 表1:方向和相应的x和y增量 Direction xInc yInc (this is not a code) Right 'R' 1 0 Left 'L' -1 0 Up 'U' 0 -1 Down 'D'
int xInc = 1,-1, 0, 0
int yInc = 0, 0, -1, 1
首先,错误来自您用于声明变量的语法:
int xInc = 1; // variable xInc can take only one value (1) and it should be an integer.
同样适用于yInc
如果您打算使用Java,那么最好从它开始思考
您想要实现的是存储带有X轴和Y轴坐标以及方向的移动
在类中启动此模型:
public class Move {
private int xcoordinate;
private int ycoordinate;
private String direction // better if it a enum
public Move(int xInc, int yInc, String direction) {
this.xcoordinate = xInc;
this.ycoordinate = yInc;
this.direction = direction;
}
// getter
public String getDirection() {
return direction;
}
}
在类中建模移动时,可以使用它进行移动!要获取信息,请利用getter
Move firstMove = new Move(2, 1, "R"); // move 2 in x axis and 1 in y axis, in right direction
Move secondMove = new Move(3, -1, "L"); // move 3 in x axis and -1 in y axis, in left direction
您可以知道如何摆脱moveBy()
方法,因为Move类本身嵌入了Move进程
取而代之的是使用一种方法
makeMove(Move Move)
,并在其中实现移动过程。您是否能够完成此代码的循环,从而使木棍图形实际移动?您好,谢谢您的帮助。我实际上在做学徒,这有点像我老板给我的作业。我基本上必须遵守指导方针。所以我必须保持moveBy,我已经有了一个名为StickFigure的类,它包含变量circle、triangle和rectangle,它们代表头部、身体和腿部。以及私有int XPO和YPO。那么现在我该怎么做这些改变呢?jzk broI需要更多细节来帮助你吗?你必须使用moveBy方法吗?是的,我必须使用该方法和表中的信息来移动我的类,包括组成类的图形、变量三角形、圆形和矩形。如果有问题中的全部信息,我将能够帮助你
Move firstMove = new Move(2, 1, "R"); // move 2 in x axis and 1 in y axis, in right direction
Move secondMove = new Move(3, -1, "L"); // move 3 in x axis and -1 in y axis, in left direction