簡體   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