繁体   English   中英

如何将char数组传递给方法并返回布尔值

[英]How to pass a char array to a method and return a boolean value

我正在尝试编写一个程序来检查一个单词是否是回文。 我的代码可以编译,但是isItPalindrome方法中的代码似乎根本没有运行。 我希望有人可以指出我出了问题的地方。 这是我的代码:

class Main
{
public static void main ( String args[] )
{
    System.out.print ("#Enter word");
    String word = BIO.getString();

    String wordLowerCase = word.toLowerCase();                                        // converts word to lower case (only way i could think of to ignore case)
    char letters[] = wordLowerCase.toCharArray();                                     // converts string into an array of character


    while ( !word.equals ("END")){
        if (isItPalindrome(letters) == true)
        {
            System.out.print (""+word+"");
            System.out.println ("   is a palindrome");

        }
        else if (isItPalindrome(letters) == false)
        {
            System.out.print (""+word+"");
            System.out.println ("   is not a palindrome");
        }

        System.out.print ("#Enter word");
        word = BIO.getString();

    }

}

public static boolean isItPalindrome ( char letters[]){

    boolean palindrome = true;
    if (letters.length%2 == 0){
        for (int index=0; index<letters.length/2-1; index++)                              //index will finish at letters/2  
        {
            if (letters[index] != letters[letters.length-index-1])                                       //checks if index is the same as its opposite letter
            {
                return false;

            }
            else                                                                         //if any pairs of letters are not in ascending order then it returns fasle and breaks out of the loop
            {
                palindrome = true;

            }

        }
    }
    else{
        for (int index = 0; index < (letters.length-1)/2-1; index++)
        {
            if (letters[index] != letters[letters.length-index-1]){

                return false;

            }
            else{

                palindrome = true;
            }  

        }
    }

    return palindrome;
}

}

除了首letters之外,字符数组letters也不会在您的main方法内的while循环中反映当前word 当您说您的方法“似乎根本没有运行”时,我不知道您的意思,但我认为您的意思是说它正在以错误的输出运行。 这就是原因之一。 一定要打电话

wordLowerCase = word.toLowerCase();
letters = wordLowerCase.toCharArray();

while循环的每次迭代。

旁注:在isItPalindrome方法中,您甚至根本不需要任何else语句或布尔palindrome 如果您发现单词不是像您现在这样的回文词的情况,则只需return false即可。 然后,如果您到达单词的结尾,则必须是回文,并且可以return true

主方法中存在一些问题,例如char数组从未更新,而回文法则非常复杂,这是工作版本:

public static void main(String args[]) {
    System.out.print("#Enter word: ");
    String word = BIO.getString();

    while (!word.equals("END")) {
        char[] letters = word.toLowerCase().toCharArray();

        if (isItPalindrome(letters) == true) {
            System.out.println(word + "   is a palindrome");
        } else {
            System.out.print(word + "   is not a palindrome");
        } // OR
        // Use this one-liner instead of the if:
        // System.out.println(word + isItPalindrome(letters) ?
        //                     "   is a palindrome" : "   is not a palindrome");

    System.out.print("#Enter word: ");
    word = BIO.getString();
    }

}

public static boolean isItPalindrome(char letters[]) {
    for (int i = 0; i < letters.length / 2; i++) {
        if (letters[i] != letters[letters.length - i - 1]) {
            return false;
        }
    }

    return true;
}

希望这可以帮助。

首先,回答标题中的问题:

public boolean palindrome(char C)

返回类型为boolean但要求您向其传递一个字符。

第二:

您通过颠倒单词来检查它们的方式使事情变得过于复杂。 您可以简单地将单词这个字符串作为一个字符串,然后将其拆开,将其放入一个Characters数组中。 然后将每个char变量放入Stack 然后,当您将它们从堆栈中逐段取出时,将它们复制到另一个Character数组中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM