为什么这个给定的数组不返回false。

huangapple go评论87阅读模式
英文:

why wont this return false with the given array

问题

int[] array2 = new int[]{1, 2, 6, 6, 3, 1};
// 该方法检查给定的数组内容在数组反转时是否保持不变
public static boolean verify(int[] array, int index) { // 方法接受数组和索引号

    if ((array[index] == array[array.length - index - 1]) && (index < array.length / 2)) {
        System.out.print("true/");
        verify(array, ++index); // 增加索引号以检查数组中的下一个值
        return true;
    } else
        System.out.println("..false..");
    return false;
}
英文:
int[] array2 = new int[]{1,2,6,6,3,1,};
//method checks if the given array contents remain the same when array is reversed
public static boolean verify(int[] array, int index) {//method takes array and an index number

    if ((array[index] == array[array.length - index-1]) &amp;&amp; (index &lt; array.length/2)) {
        System.out.print(&quot;true/&quot;);
        verify(array, ++index);// increase index number to check the next values in the th array
        return true;
    } else
        System.out.println(&quot;..false..&quot;);
        return false;
    }

答案1

得分: 0

你可能想要像这样做:

public static boolean verify(int[] array, int index) {

    if (array[index] == array[array.length - index - 1]) {
        if (index < array.length / 2)
            return verify(array, ++index);
        return true;
    } else {...}
    return false;
}

你需要确保子值传播到父值,还需要以不同的方式检查索引:如果索引超过数组长度的一半,也不算错误,你只是不想检查这种索引的值。

英文:

You probably want to do something like this:

public static boolean verify(int[] array, int index) {

    if (array[index] == array[array.length - index-1]) {
        if(index &lt; array.length/2)
            return verify(array, ++index);
        return true;
    } else {...}
    return false;
}

You need to make sure the child value gets propagated to parent, and also check the index differently: It's not wrong, if the index is over a half of the array's length, you just don't want to check the values for such index.

答案2

得分: 0

最好的做法是在方法中不要打印结果,而只打印返回值。
public static boolean verify(int[] array, int index) {
    // 不要超过中间位置
    if (index >= array.length/2) {
        return true;
    }
    // 一旦发现比较不相等的情况就立即返回 false
    if(array[index] != array[array.length-index-1]) {
        return false;
    }
    // 尝试下一个值
    return verify(array, index + 1);
}

System.out.println(verify(new int[] {1,2,3,3,3,2,1}, 0));
System.out.println(verify(new int[] {1,2,3,3,2,1}, 0));
System.out.println(verify(new int[] {1,2,4,3,3,2,1}, 0));
System.out.println(verify(new int[] {1,2,4,3,3,4,2,1}, 0));
System.out.println(verify(new int[] {1}, 0));
System.out.println(verify(new int[] {1,2}, 0));
输出结果:
true
true
false
true
true
false
英文:

It's best to not print the result in the method but to just print the return value.

public static boolean verify(int[] array, int index) {
	// don&#39;t go passed the middle
    if (index &gt;= array.length/2) {
		return true;
	}
    // return as soon as a false comparison is found
	if(array[index] != array[array.length-index-1]) {
		return false;
	}
    // Try the next value
	return verify(array, index + 1);
}

System.out.println(verify(new int[] {1,2,3,3,3,2,1}, 0));
System.out.println(verify(new int[] {1,2,3,3,2,1}, 0));
System.out.println(verify(new int[] {1,2,4,3,3,2,1}, 0));
System.out.println(verify(new int[] {1,2,4,3,3,4,2,1}, 0));
System.out.println(verify(new int[] {1}, 0));
System.out.println(verify(new int[] {1,2}, 0));

Prints

true
true
false
true
true
false

</details>



huangapple
  • 本文由 发表于 2020年9月14日 03:39:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/63874852.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定