简体   繁体   English

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

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

I am trying to write a program which checks whether a word is a palindrome or not. 我正在尝试编写一个程序来检查一个单词是否是回文。 My code compiles butthe code in the isItPalindrome method doesn't seem to be run at all. 我的代码可以编译,但是isItPalindrome方法中的代码似乎根本没有运行。 I'm hoping someone can point out where i have gone wrong. 我希望有人可以指出我出了问题的地方。 Here is my code: 这是我的代码:

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;
}

} }

Besides the initial word, your character array letters never reflects the current word in your while loop inside your main method. 除了首letters之外,字符数组letters也不会在您的main方法内的while循环中反映当前word I don't know what you mean when you say your method "doesn't seem to be run at all" but I assume that you meant it's being run with the wrong output. 当您说您的方法“似乎根本没有运行”时,我不知道您的意思,但我认为您的意思是说它正在以错误的输出运行。 This is one reason why. 这就是原因之一。 Be sure to call 一定要打电话

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

every iteration of the while loop. while循环的每次迭代。

Side note: In your isItPalindrome method, you do not even need any of the else statements or the boolean palindrome at all. 旁注:在isItPalindrome方法中,您甚至根本不需要任何else语句或布尔palindrome Simply return false if you ever find a case when the word is not a palindrome like you currently are. 如果您发现单词不是像您现在这样的回文词的情况,则只需return false即可。 Then, if you have gotten to the end of the word, it must be a palindrome and you can return true . 然后,如果您到达单词的结尾,则必须是回文,并且可以return true

There were some issues in the main-method, for example the char-array was never updated and the palindrome-method was far to complicated, here is the working version: 主方法中存在一些问题,例如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;
}

Hope this helps. 希望这可以帮助。

First, to answer the question that's in the title: 首先,回答标题中的问题:

public boolean palindrome(char C)

Has a return type of boolean but requires that you pass it a Character. 返回类型为boolean但要求您向其传递一个字符。

Second: 第二:

You're over-complicating things in the way that you reverse the words to check them. 您通过颠倒单词来检查它们的方式使事情变得过于复杂。 You could simply take the word, as a String, and take it apart, putting it into an array of Characters . 您可以简单地将单词这个字符串作为一个字符串,然后将其拆开,将其放入一个Characters数组中。 Then put each char variable into a Stack . 然后将每个char变量放入Stack Then as you take them out of the stack, piece by piece, copy them to another array of Character s. 然后,当您将它们从堆栈中逐段取出时,将它们复制到另一个Character数组中。

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

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