我必须使用 isPalindrom() 方法比较数组中的 boolean 值。如果数组是或不是回文,我无法让我的程序准确返回答案。当我故意使用非回文答案时,它总是返回 true 而不是 false。 代码:
public Boolean isPalindrome()
{
Boolean result = true;
for(int i=0;i<bits.length;i++)
{
Boolean a = bits[i];
Boolean b = bits[bits.length - i - 1];
if(a!=b)
result = false;
}
return result;
}
最佳答案
试试下面的代码:
public class TestC {
public static void main(String[] args) {
Boolean[] bits = { Boolean.FALSE, Boolean.TRUE, Boolean.FALSE,
Boolean.FALSE };
Boolean[] bits1 = { Boolean.TRUE, Boolean.TRUE, Boolean.TRUE,
Boolean.TRUE };
Boolean[] bits2 = { Boolean.FALSE, Boolean.TRUE, Boolean.FALSE };
System.out.println(isPalindrome(bits));
System.out.println(isPalindrome(bits1));
System.out.println(isPalindrome(bits2));
}
public static Boolean isPalindrome(Boolean[] bits) {
Boolean result = true;
for (int i = 0; i < bits.length; i++) {
Boolean a = bits[i];
Boolean b = bits[bits.length - i - 1];
if (a != b)
result = false;
}
return result;
}
}
关于java - boolean 数组值比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35052315/