基于秒数控制循环的Java代码计时与坐标计算异常问题
Java轨迹移动代码问题排查与解决
问题描述
- 计时不符:通过
int n = 1580;//number of moves;指定预期运行秒数,但实际总秒数与预期偏差明显,比如n=10时实际输出12秒,大数值测试偏差更显著。 - 坐标计算错误:X、Y坐标结果不稳定,例如n=10时预期坐标X=0、Y=3,但实际得到X=3、Y=1。
代码实现
public class Steps { public static void main(String[] args) { int forward1, left1; forward1 = left1 = 1; int forward2, left2, back, right; left2 = back = 0; forward2 = right = 1; //counts for X coordinate int countLeft = 0;//count moves left moves int countRight = 0;//count moves right moves //counts for Y coordinate int countForward = 0;//count moves forward moves int countBack = 0;//count moves right moves int n = 11;//number of moves; int i = 1;//iterator int all = 0;//count total seconds for(i=1; i<n;i++) {//the algorithm focuses on steps and not seconds. if(i<n) { System.out.println("Forward " + forward1); left2 += 1; i+=1; all++; //System.out.println(countForward); if(i<n) { for(int j = 0; j < left2; j++) { System.out.println("left: " + left2); countLeft++; all++;//count seconds } back += 1; i+=1; //System.out.println(countLeft); if(i<n) { for(int j=0;j<back;j++) { System.out.println("back: " + back); countBack++; all++; //System.out.println(countBack); } left2 += 1; i+=1; back += 1; if(i<n) { System.out.println("left: "+left1); forward2++; i+=1; all++;//count seconds //System.out.println(countLeft); countLeft++; //System.out.println(countLeft); if(i<n) { for(int j = 0; j < forward2; j++) { System.out.println("forward: " + forward2); countForward++; all++; } forward2++; i+=1; right +=1; if(i<n) { for(int j =0; j<right;j++) { System.out.println("right: "+right); countRight++; all++; } right +=1; i+=1; } } } } } } } //System.out.println("Y"+((countForward )-countBack)); //in order to find the X co-ordinates we need to minus //the right moves from the left moves //System.out.println(countLeft); //System.out.println("right "+right); ////in order to find the Y co-ordinates we need to minus //the forward moves from the backward moves //System.out.println("front " + countForward); //System.out.println("back "+countBack); System.out.println("after " + (all) + " seconds"); System.out.println("Coordinate X: " + (countLeft - countRight)); System.out.println("Coordinate Y: " + (countForward - countBack )); }}
运行示例(n=10)
Forward 1 left: 1 back: 1 left: 1 forward: 2 forward: 2 right: 2 right: 2 Forward 1 left: 3 left: 3 left: 3 after 12 seconds Coordinate X: 3 Coordinate Y: 1
预期算法逻辑
代码计划按照以下方框轨迹移动逻辑执行:
forward = 1 left = 1 back = 1 left = 1 forward = 2 right = 2 //end of loop forward = 1 left = 3 back = 3 left = 1 forward = 4 right = 4 //end of loop forward = 1 left = 5 back = 5 left = 1 forward = 6 right = 6
问题根源与修复方案
1. 计时不符问题
- 原因:外层
for循环的迭代逻辑混乱,嵌套的if(i<n)判断和手动递增i导致循环次数失控,同时内层循环的执行未被纳入n的计数范围,小数值测试时可能刚好匹配,但大数值时偏差持续累积。 - 修复:将
n定义为总步数上限,摒弃手动修改i的方式,改为每执行一步(包括内层循环的每一次迭代)前先检查是否超过n,一旦满足则立即终止所有循环。例如,在每个动作执行前添加if (all >= n) break;,并通过标记位跳出外层循环。
2. 坐标计算错误问题
- 原因:
- 变量管理混乱:
countBack的注释标注错误,且部分变量(如left2、back)的递增时机与预期算法不符,导致动作计数偏差。 - 动作重复执行:预期算法中的单个动作(如
back=1)被错误放在循环中重复执行,使得计数远超出预期值。
- 变量管理混乱:
- 修复:
- 修正变量注释与初始化逻辑,确保
countBack仅统计后退动作。 - 严格按照预期算法的步骤更新变量,比如每个循环周期中
left、back的取值严格匹配算法定义(第一个周期left=1、back=1,第二个周期left=3、back=3),避免无意义的递增操作。 - 确保每个动作仅执行一次并对应计数,比如执行一次
back就执行countBack++,而非在循环中多次累加。
- 修正变量注释与初始化逻辑,确保
内容的提问来源于stack exchange,提问作者1amCharlie
相关产品推荐
相关产品推荐

