我很难让循环在秒上运行int n = 1580;//number of moves;
应该是秒数,但我得到的秒数输出不同。
我也很难让X和Y坐标正确,有时坐标正确,但有时不正确。
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秒时,时间就不奇怪了。例如,当我运行程序n=10时,我得到12秒。我得到以下输出:
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
X坐标应为0,Y=3;
该算法的工作原理如下。
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
它会创建一个长方体。
一旦代码运行了一定的秒数,我需要能够提供它停止位置的X、Y坐标。
我最终使用调试器找到了问题,这个问题已经解决了,这要归功于
所以在运行调试器后,我发现我的X和Y坐标是正确的。然后,我在i
变量上运行调试器。i
注意到,当程序在三个for循环中的一个循环中迭代时,直到循环完成后,它才检查i
变量是否大于n
。这导致程序超过n
,因此我们计算的秒数比n多。下面的代码解决了这个问题:
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
//n = number of seconds this number will
//have 1 added to it
int n = 43;
int i = 0;//iterator
int all = 0;//count total seconds
while(i<n) {//algorithm start
if(i<n) {
System.out.println("Forward " + forward1);
left2 += 1;
all++;
i++;
countForward++;
//System.out.println(countForward);
if(i<n) {
for(int j = 0; j < left2; j++) {
//added if to make sure loop ends
//before i is larger than n
if(i<n) {
System.out.println("left: " + left2);
countLeft++;
all++;//count seconds
i++;
}
}
back += 1;
//System.out.println(countLeft);
if(i<n) {
for(int j=0;j<back;j++) {
//added if to make sure loop ends
//before i is larger than n
if(i<n) {
System.out.println("back: " + back);
countBack++;
all++;
i++;
//System.out.println(countBack);
}
}
left2 += 1;
back += 1;
if(i<n) {
System.out.println("left: "+left1);
forward2++;
all++;//count seconds
i++;
//System.out.println(countLeft);
countLeft++;
//System.out.println(countLeft);
if(i<n) {
for(int j = 0; j < forward2; j++) {
//added if to make sure loop ends
//before i is larger than n
if(i<n) {
System.out.println("forward: " + forward2);
countForward++;
all++;
i++;
}
}
forward2++;
right +=1;
if(i<n) {
for(int j =0; j<right;j++) {
//added if to make sure loop ends
//before i is larger than n
if(i<n) {
System.out.println("right: "+right);
countRight++;
all++;
i++;
}
}
right +=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+1) + " seconds");
System.out.println("Coordinate X: " + (countLeft - countRight));
System.out.println("Coordinate Y: " + (countForward - countBack ));
}}
感谢那些通知我使用调试器的人。似乎当你在盒子里的时候,很难跳出盒子思考。
代码中的while循环当前正在运行,直到i
大于或等于n
。现在,如果要将n
更改为程序应该运行的秒数,则需要将i
更改为程序已经运行的秒。最有可能的是,无论你在什么平台上运行这个程序,都需要不到1850秒的时间来完成运行,所以当你运行它的时间更长时,你会多次迭代while循环,从而改变你得到的结果。