每次循环运行时,变量的值都会不断重置(Java)
我目前正在为我的Java OOP类做家庭作业。任务是一只老鼠被困在30英尺长的管道中间。鼠标需要随机移动,向左或向右移动的几率为50%。对于每次移动,鼠标只能在最小3英尺和最大8英尺之间随机移动距离。程序应在每次移动后跟踪鼠标与管道左右两端的距离,一旦鼠标到达管道的一端,循环应结束,并应打印出鼠标已到达任何一端。我使用变量fromendL跟踪鼠标与左端的距离,并使用fromendR跟踪另一端的距离。我遇到的问题是,每次循环运行时,它都会重置每个变量的值,因此它永远不会给出多个循环过程中的距离。我相信我拥有的一切都很好,尽管这可能不是最有效的方式(我对编程还是相当陌生) 我将在下面附上代码:每次循环运行时,变量的值都会不断重置(Java),java,loops,random,distance,Java,Loops,Random,Distance,我目前正在为我的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!");
}
}
}
}
}
主类{
公共静态void main(字符串[]args)
{
int fromendL;
int fromendR;
int-tMin=3;
int tMax=8;
int方向;
国际旅行;
int-travelR=15;
国际旅行社=15;
布尔值=假;
而(!逃脱)
{
方向=(int)(Math.random()*2+1);
行程=(int)(Math.random()*(tMax-tMin)+3);
如果(方向==1)
{
fromendL=15-行程;
fromendR=30-15+行程;
travel=travel-travel;
System.out.println(“鼠标已移动:“+travel+”ft向左”);
System.out.println(“鼠标距离左端“+fromendL+”英尺”);
System.out.println(“鼠标距离右端“+fromendR+”英尺”);
如果(行程==0 | |行程<0)
{
逃逸=真;
System.out.println(“鼠标从管道的左端逃脱了!”);
}
否则如果(行程==30 | |行程>30)
{
逃逸=真;
System.out.println(“鼠标从管道的右端逃脱了!”);
}
}
否则如果(方向==2)
{
fromendL=15+行程;
fromendR=30-15-行程;
旅行者=旅行者+旅行;
System.out.println(“鼠标已移动:“+travel+”ft向右”);
System.out.println(“鼠标距离左端“+fromendL+”英尺”);
System.out.println(“鼠标距离右端“+fromendR+”英尺”);
如果(行程==30 | |行程>30)
{
逃逸=真;
System.out.println(“鼠标从管道的右端逃脱了!”);
}
否则如果(行程==0 | |行程<0)
{
逃逸=真;
System.out.println(“鼠标从管道的左端逃脱了!”);
}
}
}
}
}
以下代码应能解决原始问题。请对其进行分析,找出与代码相比的一些改进
我没有使用两个变量来保持位置(fromendL
和fromendR
),而是使用一个变量(pos
)。如果我知道管道的长度(不变)和从管道左侧开始的位置,可以计算到右端的距离
像if(a==0 | | a<0)
这样的构造编写得更像if(a 0?“right”:“left”);
System.out.println(“鼠标距离左端“+pos+”英尺”);
System.out.println(“鼠标距离右端“+(pLength-pos)+”英尺\n”);
如果(pos>=pLength | | pos我感谢您的全面回复!我还想知道您是否能够向我解释一下您编写的(pos我编辑了我的答案。可能需要很长时间才能发表评论
fromendL = 15 - travel;
fromendR = 30 - 15 + travel;
travelL = travelL - travel;
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;
}
}
}
}