有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java偶数回文

我一直在写回文,它不支持偶数个单词。我不擅长编码。它支持“racecar”或“Tacocate”等词,但不允许我使用“Hannah”等词/名称。我对这种编码技术还不熟悉,所以任何东西都非常感谢


import java.util.Scanner;
public class Palindrome
{
    public static void main(String args [])
    {
        System.out.printf("\f");
        Scanner input = new Scanner(System.in);
        System.out.println("enter a word");
        String word = input.nextLine();
        int size = word.length();
        int correct = 0;
        int incorrect = 0;
        for (int count = 1; count < size; count++)
        {
            int start = (word.charAt(count));//starting
            int end = (word.charAt(size-count));//ending
            if (start == end)
                correct++;
            else
                incorrect++;
        }
        if (correct == 0)
            System.out.printf("%s is a palindrome", word);
        else 
            System.out.printf("%s is not a palindrome", word);
    } 
}

共 (5) 个答案

  1. # 1 楼答案

    首先,您应该知道java中的数组从0开始,而不是从1开始。因此,将计数从0设置为1
    那么,word.charAt(count)是一个char,所以最好使用char变量而不是int

    你用来判断一个单词是否是回文的算法似乎是通过匹配第一个字符和最后一个字符,第二个字符和第二个最后一个字符,等等
    如果是这样的话,你只需要半循环for (int count = 1; count < size / 2; count++)

    最后一个是,你只需要一个变量来保存回文的状态,如果你的匹配过程发现了一个false,那么打破循环,只需将isPalindrome状态设置为false

    public static void main (String args[])
    {
        Scanner input = new Scanner (System.in);
        System.out.println ("enter a word");
        String word = input.nextLine ();
        int size = word.length ();
        boolean isPalindrome = true;
        int maxIndex = size - 1;
        for (int count = 0; count < size / 2; count++)
        {
            char start = word.charAt (count);
            char end = word.charAt (maxIndex - count);
            if (start != end)
            {
                isPalindrome = false;
                break;
            }
        }
        if (isPalindrome)
            System.out.printf ("%s is a palindrome", word);
        else
            System.out.printf ("%s is not a palindrome", word);
    }  
    

    请记住,java的字符串是区分大小写的,所以“Tiger”不同于“Tiger”。因此,汉娜不会被视为回文。如果您希望它不区分大小写,只需在执行macthing过程之前将单词中的所有字符小写word = word.toLowerCase()

  2. # 2 楼答案

    检查回文函数非常简单:

    public boolean isPalindrome(String str) {
        if(str == null)
            return false;
    
        str = str.toLowerCase();
    
        for(int i = 0, j = str.length() - 1; i < j; i++, j--)
            if(str.charAt(i) != str.charAt(j))
                return false;
    
        return true;
    }
    
  3. # 3 楼答案

    您可以使用Stringbuilder执行回文检查,如下所示

    public class test {
    
    public static void main(String args [])
    {
        System.out.print("\f");
        Scanner input = new Scanner(System.in);
        System.out.println("enter a word");
        String word = input.nextLine();
    
        StringBuilder originalStr = new StringBuilder(word);        
        String revString = originalStr.reverse().toString();
    
    
        if(word.equalsIgnoreCase(revString)) 
             System.out.print( word +" is a palindrome");
       else 
           System.out.print( word +" is not a palindrome");
    } 
    }
    
  4. # 4 楼答案

    你的代码中有几个错误

    如果您打算在检查中忽略大写字母,那么应该将所有内容转换为小写,因为它在ASCII中的标识不同

    从第一个字母开始,应该从索引0开始,而不是从1开始

    结尾应该从索引size-count-1开始,而不是从最后一个字母开始

    你应该检查incorrect == 0而不是correct == 0来确定它是否是回文

    public static void main(String args[]) {
        System.out.printf("\f");
        Scanner input = new Scanner(System.in);
        System.out.println("enter a word");
        String word = input.nextLine().toLowerCase();
        int size = word.length();
        int correct = 0;
        int incorrect = 0;
        for (int count = 0; count < size; count++)
        {
            int start = (word.charAt(count)); //starting
            int end = (word.charAt(size-count-1)); //ending
            if (start == end)
                correct++;
            else
                incorrect++;
            System.out.println(start + " " + end);
        }
        if (incorrect == 0)
            System.out.printf("%s is a palindrome", word);
        else 
            System.out.printf("%s is not a palindrome", word);
    }
    

    额外好处:你可以只检查单词的一半,而不是在整个单词中循环

  5. # 5 楼答案

    你的代码有很多问题:

    1. 你在比较错误索引的字符。例如,将第二个字符(其索引为1)与最后一个字符(其索引为大小-1)进行比较count应该初始化为0,而end应该是word.charAt(size-count-1)

    2. correct == 0,当它应该是incorrect == 0(顺便说一句,你不需要计数器,只需要一个布尔值)时,报告字符串为回文

    3. 如果希望检查不区分大小写,可以在运行循环之前将字符串转换为小写

    这应该是有效的:

    public static void main(String args [])
    {
        System.out.printf("\f");
        Scanner input = new Scanner(System.in);
        System.out.println("enter a word");
        String word = input.nextLine().toLowerCase();
        int size = word.length();
        boolean isPalindrome = true;
        for (int count = 0; count < size; count++)
        {
            int start = (word.charAt(count));//starting
            int end = (word.charAt(size-count-1));//ending
            if (start != end) {
                isPalindrome = false;
                break;
            }
        }
        if (isPalindrome)
            System.out.printf("%s is a palindrome", word);
        else 
            System.out.printf("%s is not a palindrome", word);
    }