0

私の最初のJavaクラスを受講して、愚かに立ち往生しています。私は回文プロジェクトをやっています。そのロジックは良さそうです。いずれの場合も True と表示されます。私は何を間違っていますか?

電話:

boolean result = check(input);

またはメソッド自体で:

public static void display(boolean result, String palindrome)
{
    if (result = true)
    {
        JOptionPane.showMessageDialog(null, palindrome
                + " is a palindrome.");
    } else
        JOptionPane.showMessageDialog(null, palindrome
                + " is not a palindrome.");

}

コード全体は次のとおりです。 import javax.swing.JOptionPane;

public class Palindrome
{

public static void main(String args[])

{
    // declare variables

    // call methods
    String input = retrieveInput();
    boolean result = check(input);
    display(result = false, input);
    finish();
}

// Accepts and validates input
public static String retrieveInput()
{
    // declare variables
    int length;
    String palindrome = null;
    boolean done = false;
    while (!done)
    {
        try
        {
            // user input
            palindrome = JOptionPane.showInputDialog(null,
                    "Please enter a 5 digit integer:");
            length = palindrome.length();

            // data validation
            if (length != 5)
            {
                throw new NumberFormatException();
            } else
                done = true;
        }

        catch (NumberFormatException e)
        {
            JOptionPane.showMessageDialog(null,
                    "Error. Please enter a 5 digit integer", "Error",
                    JOptionPane.INFORMATION_MESSAGE);
        }
    }
    return palindrome;
}

public static Boolean check(String palindrome)
{
    // determine if palindrome

    int left = 0;
    int right = palindrome.length() - 1;

    while (left < right)
    {
        if (palindrome.charAt(left) != palindrome.charAt(right))
            return false;

        left++;
        right--;
    }

    return true;

}

// The output method displays commission and sales
public static void display(boolean result, String palindrome)
{
    if (result = true)
    {
        JOptionPane.showMessageDialog(null, palindrome
                + " is a palindrome.");
    } else
        JOptionPane.showMessageDialog(null, palindrome
                + " is not a palindrome.");

}

// finish() method exits program
public static void finish()
{
    System.exit(0);
}

}
4

3 に答える 3

3
 if (result = true)

true に設定resultし、それを評価します (再び、 as true)。使用する:

if(result==true)

また

if(result)

代わりは。前者はほとんどの値比較の構文であり、後者はブール値に対してのみ機能します。

于 2013-09-21T19:45:50.910 に答える
2

==は、値を比較するときに使用されます。

=は比較演算子ではなく代入演算子です。

だから試してください:

if (result == true)

また

if (result)

編集後の完全なコード:

public static void main(String args[])
{
    String input = retrieveInput();
    boolean result = check(input);
    display(result, input);  // change result = false 
    finish();
}
于 2013-09-21T19:48:59.920 に答える
1

この行で

if (result = true)

true変数に値を代入してresultおり、その値 ( true) が の条件として使用されており、が と等しいifかどうかをチェックしていません。つまり、あなたの意志は常にその状態を評価します。resulttrueiftrue

==演算子を使用する必要があったでしょう

if (result == true)

または単に

if (result)
于 2013-09-21T19:45:32.450 に答える