繁体   English   中英

检查字符串是否是回文(使用方法)

[英]Check if a string is a Palindrome (using methods)

我不知道为什么以下方法不起作用。 表明:

Palindrome.java:97:错误:预期<identifier>

该方法采用的参数是String,如果提供的String是回文,则应返回truefalse

public static boolean checkPalindrome(checkString) {
    boolean test = true;
    int left = 0;
    int right = checkString.length() - 1;
    while (left < right && test) {
        if (checkString.charAt(left) != checkString.charAt(right)) {
            test = false;
        }
        right--;
        left++;
    }
    return test;
}

问题是这条线

public static boolean checkPalindrome(checkString)

应该

public static boolean checkPalindrome(String checkString)

只是一个建议,但您也可以减少所使用的变量

int len = checkString.length();
for (int i = 0; i < len / 2; i++) {
    if (checkString.charAt(i) != checkString.charAt(len - i -1)) {
        return false;
    }
}
return true;

我认为您在第一行中犯了一个错误,应该是:

public static boolean checkPalindrome(String checkString)   

您必须在参数^之前提供数据类型

第一行中的错误,应该是:

public static boolean checkPalindrome(String checkString)   

您必须在参数^之前提供数据类型

暂无
暂无

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

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