int [] f = {1,2,3,4,5,5,4,3,2,1};
int [] b = {6,1};
System.out.println(Arrays.toString(hide(f,b)));
public static int [] hide(int [] front, int [] back) {
int temp;
int extraTemp;
int nextTemp = 0;
int [] hiddenAt = new int[front.length];
//int [] shownAt = new int[front.length];
for(int x = 0; x < front.length; x++){
for(int y = 0; y <= back.length; y++){
temp = x;
if ((back.length > front.length) || (front[x] < 0 || back[y] < 0) || (front.length < 1 || back.length < 1)) {
System.exit(0);
}
if (y < back.length - 1){
nextTemp = Math.abs(back[y + 1] - front[x + 1]);
}
else {
nextTemp = 0;
}
if (front[x] > back[y]) {
System.out.println(temp);
}
else if (front[x] < back[y] && y >= back.length - 1 ) {
extraTemp = back[y] - front[x];
if (extraTemp > nextTemp){
extraTemp = nextTemp;
}
System.out.println(extraTemp);
}
else if (front[x] < back[y]) {
extraTemp = back[y] - front[x];
if (extraTemp > nextTemp){
extraTemp = nextTemp;
}
System.out.println(extraTemp);
}
}
}
return hiddenAt;
}
' println
s 被替换以查看正在生成的值。hiddenAt[z] = temp;
当我找出正确的值时,它们就会出现。
我需要它front[0]
与back[0]
then front[1]
withback[1]
等进行比较。之后,它将转移到一个:front[1]
with back[1]
then front[2]
withback[2]
直到结束front[]
。我想找出最小的差异在哪里。(在这种情况下,[] 中的数字是 4 和 5。)
我收到两个错误,一个是第 39 行 if ((back.length > front.length) || (front[x] < 0 || back[y] < 0) || (front.length < 1 || back.长度 < 1)) { System.exit(0); }
是错误的,第 9 行也是错误的。公共类 2 { public static void main(String[] args) {
int [] f = {1,2,3,4,5,5,4,3,2,1};
int [] b = {6,1};
System.out.println(Arrays.toString(hide(f,b)));
}
确切的错误是:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
at hw2.hide(hw2.java:39)
at hw2.main(hw2.java:9)