问题描述
我目前正在为Java OOP类进行家庭作业。分配是将鼠标困在30英尺长的管道中途。鼠标需要随机移动,并且有50%的机会向左或向右移动。每次移动时,鼠标只能在最小3英尺和最大8英尺之间移动随机距离。该程序应跟踪每次移动后鼠标到管道左右两端的距离,一旦到达管道的一端,循环应结束,并且应打印出鼠标已到达任何位置结束了。我正在使用变量fromendL来跟踪鼠标到左端的距离,以及fromendR到另一端的距离。我遇到的问题是,每次循环运行时,它都会重置每个变量的值,因此在多个循环过程中,它永远不会给我带来距离。我相信我的一切工作都很好,尽管这可能不是最有效的方法(我对编程还是很陌生的。)
我将附上以下代码:
class Main {
public static void main(String[] args)
{
int fromendL;
int fromendR;
int tMin = 3;
int tMax = 8;
int direction;
int travel;
int travelR = 15;
int travelL = 15;
boolean escaped = false;
while (!escaped)
{
direction = (int)(Math.random()* 2 + 1);
travel = (int)(Math.random() * (tMax - tMin) + 3);
if (direction == 1)
{
fromendL = 15 - travel;
fromendR = 30 - 15 + travel;
travelL = travelL - travel;
System.out.println("The mouse has moved: " + travel + " ft to the left" );
System.out.println("The mouse is " + fromendL +" ft from The Left End");
System.out.println("The mouse is " + fromendR +" ft from The Right End\n");
if(travelL == 0 || travelL < 0)
{
escaped = true;
System.out.println("The Mouse has escaped the Left end of the Pipe!");
}
else if (travelR == 30 || travelR > 30)
{
escaped = true;
System.out.println("The Mouse has escaped the Right end of the Pipe!");
}
}
else if (direction == 2)
{
fromendL = 15 + travel;
fromendR = 30 - 15 - travel;
travelR = travelR + travel;
System.out.println("The mouse has moved: " + travel + " ft to the right");
System.out.println("The mouse is " + fromendL +" ft from The Left End");
System.out.println("The mouse is " + fromendR +" ft from The Right End\n");
if(travelR == 30 || travelR > 30)
{
escaped = true;
System.out.println("The Mouse has escaped the Right end of the Pipe!");
}
else if (travelL == 0 || travelL < 0)
{
escaped = true;
System.out.println("The Mouse has escaped the Left end of the Pipe!");
}
}
}
}
}
解决方法
下面的代码应该可以解决原始问题。请对其进行分析,以找出与您的代码相比有哪些改进。
我没有使用两个变量来保持位置(fromendL
和fromendR
),而是使用了一个(pos
)。如果我知道管子的长度(不变)和从管子左侧开始的位置,则可以计算出到右端的距离。
在我看来,像if (a == 0 || a < 0)
这样的结构最好像if (a <= 0)
这样写。
我将方向设为+1或-1。就像在数学中一样,负方向就像向左移动,正方向就像向右移动。
每次重置变量的原因在于
fromendL = 15 - travel;
fromendR = 30 - 15 + travel;
travelL = travelL - travel;
您从15 resp 30开始,一遍又一遍,而不是使用鼠标的最后一个位置。
我的代码看起来像 (我使用制表符进行缩进;我的眼睛太大了,看不到2字符的缩进;):
class Main {
public static void main(String[] args)
{
static final int tMin = 3;
static final int tMax = 8;
static final int pLength = 30;
int direction;
int travel;
int pos = pLength / 2;
boolean escaped = false;
while (!escaped) {
direction = (int)(Math.random() * 2);
if (direction == 0) direction = -1;
travel = ((int)(Math.random() * (tMax - tMin)) + tMin) * direction;
pos += travel;
System.out.println("The mouse has moved: " + Math.abs(travel) + " ft to the " + (travel > 0 ? "right" : "left"));
System.out.println("The mouse is " + pos +" ft from The Left End");
System.out.println("The mouse is " + (pLength - pos) + " ft from The Right End\n");
if (pos >= pLength || pos <= 0) {
System.out.println("The Mouse has escaped the " + (pos <= 0 ? "Left" : "Right") + " end of the Pipe!");
escaped = true;
}
}
}
}
?
是三元运算符。它测试条件,如果为true,则表达式的值直接等于问号后的值。如果为false,则表达式的计算结果为冒号后面的值。谨慎使用它,许多人不喜欢它,并认为它令人困惑。因此,我将其放在括号之间,以便清楚地知道构造的开始和结束位置。
由于pos小于或等于零(向左转义)或大于或等于30(向右转义),因此我只需要测试两者中的哪一个即可。如果我知道鼠标没有向左逃逸,那一定是向右逃逸了。 (if
的状态已保证鼠标已逃脱)。