使用阵列检查Palindromes

时间:2017-02-21 22:00:56

标签: java arrays methods palindrome

目标是取用户输入的短语并告诉他们是否是回文。它必须有一个数组和一个方法。我的数组和我的方法的返回结果有问题。我编辑了一些,但我仍然遇到布尔部分的问题。无论我进入什么地方,我都会变得虚伪。我还想确保如果输入任何空格,他们将被删除。我尝试过replaceAll但是无法让它工作。谢谢你的帮助。

import java.util.Scanner;
public class palindrome {

public palindrome() {
    // TODO Auto-generated constructor stub
}

public static void main(String[] args) {
    // TODO Auto-generated method stub

            Scanner keyboard = new Scanner(System.in);
            int another=1;
            boolean results;

            System.out.println("Please enter a phrase to be checked:");
            String input = keyboard.next();
            if (checkpalindrome(input))
                System.out.println("Yes, the phrase is a palindrome");
            else
                System.out.println("No, the phrase is not a palindrome");

            System.out.println("Would you like to try another one? \"1 or 0\"");
            another = keyboard.nextInt();

}
public static boolean checkpalindrome(String input){


    char array2 = 0;
char[] array1 = input.toCharArray();
StringBuilder sb = new StringBuilder(input.length());

for(int i=0; i<array1.length/2; i++)
{
    array2 = array1[i];
    array1[i]=array1[array1.length-1-i];
    array1[array1.length-1-i]=array2;
}

System.out.println(input);
System.out.println(array1);
System.out.println(test.equals(array1));

return(input.equals(array1));

}
}

0 个答案:

没有答案