检查字符串是否对称和回文

发布于 2025-01-20 12:07:50 字数 1865 浏览 1 评论 0原文

我刚刚去了 makeuseof 上的 Symmetrical,它目前位于javascript 和我将其转换为 Java,但是第 38 行(数组)上有一个错误。请检查下面的代码,谢谢。

import java.util.Scanner;
    class check
    {
    public static void main(String args[])
    {
        String str, rev = "";
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a string:");
        str = sc.nextLine();
        int length = str.length();
        for ( int i = length - 1; i >= 0; i-- )
            rev = rev + str.charAt(i);
        //Check if Symmetrical
        if (isSymmetrical(str)) {
            System.out.println(str +" is a symmetrical");
        } else {
            System.out.println(str +" is not a symmetrical");
        }
        //Check if palindrome
        if (str.equals(rev))
            System.out.println(str +" is a palindrome");
        else
            System.out.println(str +" is not a palindrome");
    }
    public static boolean isSymmetrical(String str){
        double midIndex;
        var length = str.length();

        if (length % 2 == 0) {
            midIndex = Math.floor(length/2);
        }
        else {
            midIndex = Math.floor(length/2) + 1;
        }
            var pointer1 = 0;
            var pointer2 = midIndex;
            while(pointer1 < midIndex && pointer2 < length) {
                if(str[pointer1] == str[pointer2]) {
                    pointer1 += 1;
                    pointer2 += 1;
                }
                else {
                    return false;
                }
            }
            return true;
    }
}

错误: 输入图片此处描述

I just go the Symmetrical at makeuseof and it's currently in javascript and i converted it to Java, However there's an error on the line 38 which is the array. Please check the code below, Thank you.

import java.util.Scanner;
    class check
    {
    public static void main(String args[])
    {
        String str, rev = "";
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a string:");
        str = sc.nextLine();
        int length = str.length();
        for ( int i = length - 1; i >= 0; i-- )
            rev = rev + str.charAt(i);
        //Check if Symmetrical
        if (isSymmetrical(str)) {
            System.out.println(str +" is a symmetrical");
        } else {
            System.out.println(str +" is not a symmetrical");
        }
        //Check if palindrome
        if (str.equals(rev))
            System.out.println(str +" is a palindrome");
        else
            System.out.println(str +" is not a palindrome");
    }
    public static boolean isSymmetrical(String str){
        double midIndex;
        var length = str.length();

        if (length % 2 == 0) {
            midIndex = Math.floor(length/2);
        }
        else {
            midIndex = Math.floor(length/2) + 1;
        }
            var pointer1 = 0;
            var pointer2 = midIndex;
            while(pointer1 < midIndex && pointer2 < length) {
                if(str[pointer1] == str[pointer2]) {
                    pointer1 += 1;
                    pointer2 += 1;
                }
                else {
                    return false;
                }
            }
            return true;
    }
}

Error:
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

当梦初醒 2025-01-27 12:07:50

感谢您分享 string :: charat 这很有帮助

,我使用了charat并将double转换为int。

import java.util.Scanner;
    class check
    {
    public static void main(String args[])
    {
        String str, rev = "";
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a string:");
        str = sc.nextLine();
        int length = str.length();

        for ( int i = length - 1; i >= 0; i-- )
            rev = rev + str.charAt(i);
        //Check if Symmetrical
        if (isSymmetrical(str)) {
            System.out.println(str +" is a symmetrical");
        } else {
            System.out.println(str +" is not a symmetrical");
        }
        //Check if palindrome
        if (str.equals(rev))
            System.out.println(str +" is a palindrome");
        else
            System.out.println(str +" is not a palindrome");
    }
    public static boolean isSymmetrical(String str){
        double midIndex;
        int length = str.length();
        
        if (length % 2 == 0) {
            midIndex = Math.floor(length/2);
        }
        else {
            midIndex = Math.floor(length/2) + 1;
        }
            int pointer1 = 0;
            double pointer2 = midIndex;
            while(pointer1 < midIndex && pointer2 < length) {
                if(str.charAt(pointer1) == str.charAt((int)pointer2)) {
                    pointer1 += 1;
                    pointer2 += 1;
                }
                else {
                    return false;
                }
            }
            return true;
    }
}

输出:

Thanks to for sharing the String::charAt this is very helpful

I used charAt and convert double to int.

import java.util.Scanner;
    class check
    {
    public static void main(String args[])
    {
        String str, rev = "";
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a string:");
        str = sc.nextLine();
        int length = str.length();

        for ( int i = length - 1; i >= 0; i-- )
            rev = rev + str.charAt(i);
        //Check if Symmetrical
        if (isSymmetrical(str)) {
            System.out.println(str +" is a symmetrical");
        } else {
            System.out.println(str +" is not a symmetrical");
        }
        //Check if palindrome
        if (str.equals(rev))
            System.out.println(str +" is a palindrome");
        else
            System.out.println(str +" is not a palindrome");
    }
    public static boolean isSymmetrical(String str){
        double midIndex;
        int length = str.length();
        
        if (length % 2 == 0) {
            midIndex = Math.floor(length/2);
        }
        else {
            midIndex = Math.floor(length/2) + 1;
        }
            int pointer1 = 0;
            double pointer2 = midIndex;
            while(pointer1 < midIndex && pointer2 < length) {
                if(str.charAt(pointer1) == str.charAt((int)pointer2)) {
                    pointer1 += 1;
                    pointer2 += 1;
                }
                else {
                    return false;
                }
            }
            return true;
    }
}

Output:
enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文