0

ユーザーの入力が回文かどうかを確認する Java プログラムを作成しています。私のコードは以下にありますが、次のとおりです。

if(isPalindrome() = true)
     System.out.println("You typed a palindrome!");

一部「割り当ての左側は変数でなければなりません」というエラーが表示されます。変数じゃない?それを修正するにはどうすればよいですか?どんなアドバイスでも大歓迎です!

public class PalindromeChecker
{
public static void main(String [] args)
{
    String answer;
    while(answer.equalsIgnoreCase("y"))
    {
        System.out.println("Please enter a String of characters.  I will check to see if");
        System.out.println("what you typed is a palindrome.");
        Scanner keys = new Scanner(System.in);
        String string = keys.nextLine();
        if(isPalindrome() = true)
            System.out.println("You typed a palindrome!");
        else
            System.out.println("That is not a palindrome.");
        System.out.print("Check another string? Y/N: ");
        answer = keys.next();
    }
}

public static boolean isPalindome(String string)
{
    if(string.length() <= 0)
        System.out.println("Not enough characters to check.");
    string = string.toUpperCase();
    return isPalindrome(string,0,string.length()-1);
}

private static boolean isPalindrome(String string, int last, int first)
{
    if(last <= first)
        return true;
    if(string.charAt(first) < 'A' || (string.charAt(first) > 'Z'))
        return isPalindrome(string,first + 1, last);
    if(string.charAt(last) < 'A' || (string.charAt(last) > 'Z'))
        return isPalindrome(string,first, last - 1);
    if(string.charAt(first) != string.charAt(last))
        return false;
    return isPalindrome(string,first + 1, last - 1);
}
}
4

3 に答える 3

3

==比較には double equals を使用します。単一の等号=は代入演算子です。

if (isPalindrome() == true)

またはさらに良いことに、ブール比較ではまったく使用==しないでください。次のように書くだけで、英語のように読めます。

if (isPalindrome())
于 2012-11-28T00:45:38.793 に答える
1

メソッド呼び出しは次のようにする必要があります: isPalindrome 予期される文字列パラメーター:

if(isPalindome(string ))

また、戻り値の型はブール値であるため、等価チェックを行う必要はありません。

于 2012-11-28T00:45:55.923 に答える
0

使用する

if(isPalindome(string)==true)

その代わり。

2 つの変更:

1) に渡す必要がありstringますisPalindome

2) 比較のために、1 つだけでなく 2 つの等号を使用する必要があります。

また、「isPalindome」ではなく「isPalindrome」と書くつもりだったのかもしれません。

于 2012-11-28T00:46:02.100 に答える