for循环中的char数组:Palindrome程序

时间:2013-12-12 20:07:48

标签: arrays for-loop char

我无法运行以下代码,但我不确定为什么。我试图编写以下代码,以输出char数组是否是回文。感谢

public static void main(String[] args) {

        boolean palindrome = false;

        char[] c = { 'a', 'b', 'b', 'a' };

        int n = c.length;

        for (int i = 0; i < (c / 2) + 1; i++) {

            if (c[i] != (n - c - 1)) {
                palindrome = false;
            }
        }

        palindrome = true;

        System.out.println(palindrome);

    }// main end

}// class end

3 个答案:

答案 0 :(得分:1)

您在if语句中将字符与未定义的表达式进行比较。 c[i]是一个字符(本例中为'a'或'b'),但未定义n - c - 1,因为您无法从整数中减去字符数组(c) (n)。

您还尝试将字符除以2,这在您的for语句中未定义。它应该是i < (n/2)

答案 1 :(得分:1)

更改

for (int i = 0; i < (c / 2) + 1; i++)

for (int i = 0; i < (c / 2); i++

然后改变

if (c[i] != (n - c - 1)) {

if (c[i] != c[n-i-1]) {

用你的例子来检查:

c[0] == c[4-0-1] // i is 0, so c[0] == c[3], increment i to 1
c[1] == c[4-1-1] // i is 1, so c[0] == c[2], increment i to 2
//exit the loop as i (2) is not less than (4/2)

它也适用于奇数个字符。

答案 2 :(得分:1)

public class Palindrome {
    public static void main(String[] args) {
        testPalindrome();
    }

    public static boolean isPalindrome(String str) {
        char[] c = str.toCharArray();
        int n = c.length - 1;
        int i = 0;
        while (i < n) {
            if (c[i] != c[n]) {
                return false;
            }
            i++;
            n--;
        }
        return true;
    }

    // This method tests the isPalindrome method
    // if any test fails a message will be printed and the program terminated
    public static void testPalindrome() {
        assertTrue(isPalindrome("abba"), 
                "Failed asserting 'abba' is a Palindrome");
        assertTrue(isPalindrome("abcba"), 
                "Failed asserting 'abcba' is a Palindrome");
        assertTrue(isPalindrome("aaaa"), 
                "Failed asserting 'aaaa' is a Palindrome");
        assertTrue(isPalindrome("#171#"), 
                "Failed asserting '#171#' is a Palindrome");
        assertTrue(isPalindrome("AbccbA"), 
                "Failed asserting 'AbccbA' is a Palindrome");
        assertTrue(isPalindrome("AAaAA"), 
                "Failed asserting 'AAaAA' is a Palindrome");

        assertTrue(! isPalindrome("abcbab"), 
                "Failed asserting 'abcbab' is NOT a Palindrome");
        assertTrue(!isPalindrome("Abccba"), 
                "Failed asserting 'Abccba' is NOT a Palindrome");
        assertTrue(!isPalindrome("Theses are tests"), 
                "Failed asserting 'Theses are tests' is NOT a Palindrome");
        assertTrue(!isPalindrome("AAAa"), 
                "Failed asserting 'AAAa' is NOT a Palindrome");
        assertTrue(!isPalindrome("AAAav"), 
                "Failed asserting 'AAAav' is NOT a Palindrome");
        assertTrue(!isPalindrome("12345678909876543210"), 
                "Failed asserting '12345678909876543210' is NOT a Palindrome");
    }

    public static void assertTrue(boolean test, String failMsg) {
        if (!test) {
            System.out.println(failMsg);
            System.exit(1);
        }
    }
}