0

好的,我知道我在这里遗漏了一些简单的东西,但是我找不到我的错误。该程序运行良好,它只是返回错误消息。我没有正确称呼它或其他什么。我可以从 display() 方法中删除代码并将其放入 check() 方法中,它可以完美运行。有人想帮助一个被卡住的新手吗?我不确定是否需要显示整个程序,如果不需要,我很抱歉。有几次我被告知我没有放入足够的代码。我还希望对我编写代码的方式提供任何建设性的反馈,因为我不想养成任何坏习惯。

public class Palindrome {

    // Main Method
    public static void main(String[] args) {

        // Declare variables
        int number;

        // Call method
        number = retrieveInput();
        check(number);
        display();


    }// End main method
    //*************************************************************************************
    // Method to retrieve the input
    public static int retrieveInput(){

        //Declare variables
        String number = null;
        int numInput = 0;
        boolean done = false;

        // Prompt user for input and validate that input
        while(!done){
            try{
                number = JOptionPane.showInputDialog("Enter 5 single digits");
                numInput = Integer.parseInt(number);
                if (numInput <10000 || numInput > 99999) 
            throw new NumberFormatException();
                else 
                    done = true;
            }

            catch(NumberFormatException e)
            {
                JOptionPane.showMessageDialog(null, "Entry invalid. Please re-enter 5 single digits", "Error", JOptionPane.ERROR_MESSAGE);
            }

            }

        return numInput;

    }// End retrieveInput method
    //*************************************************************************************
    // Method to determine if input is a palindrome or not
    public static boolean check(int number){

        //Declare variables
        int num1;
        int num2;
        int num4;
        int num5;
        boolean isPalindrome = false;

        num1 = number / 10000;
        num2 = number % 10000 / 1000;
        num4 = number % 100/10;
        num5 = number % 10;

        // Checking to see if input is a palindrome
        if (num1 == num5 && num2 == num4);
        {
        isPalindrome = true;
        }

        if (num1 != num5 && num2 != num4);
        {
        isPalindrome = false;
        }


        return isPalindrome;

    }// End check method
    //*************************************************************************************
    // Method to display results
    public static void display(){

        // Declare variable
        boolean isPalindrome = false;


        // Display results
        if (isPalindrome == true)
            {
            JOptionPane.showMessageDialog(null, "These 5  digits are a palindrome", "Results", JOptionPane.INFORMATION_MESSAGE);
            }

            else 
            {
            JOptionPane.showMessageDialog(null, "These 5  digits are NOT a palindrome", "Results", JOptionPane.INFORMATION_MESSAGE);
            }


    } // End display method 
        //************************************************************************************* 
} // End class Palindrome
4

4 回答 4

3

Change the last two lines of main to read

boolean isPalindrome = check(number);
display(isPalindrome);

Change the declaration of display to

public static void display(boolean isPalindrome){

and remove the line in display that says

boolean isPalindrome = false;

That way, the isPalindrome that display works with will be the same one that was evaluated in check.

Edit:

Also remove the semicolon at the end of

if (num1 != num5 && num2 != num4);

otherwise the block underneath it will run in every case, and set isPalindrome back to false. Alternatively, you could just remove this condition and the block below it entirely, because it actually doesn't do anything.

于 2013-09-14T01:47:41.153 回答
0

哦,我的上帝,您在检查方法中做了一些非常愚蠢的事情。你说那isPalindrome是假的,然后显示的值isPalindrome没有考虑到isPalindrome一个非常不同的方法计算的事实,你没有办法得到它的值。你可以做的是你用来取出retrieveInput和输入数字的相同方法check:函数参数。这是一个修改后的display方法:

public static void display(boolean isPalindrome){

    // Declare variable
    boolean isPalindrome = false;


    // Display results
    if (isPalindrome == true)
        {
        JOptionPane.showMessageDialog(null, "These 5  digits are a palindrome", "Results", JOptionPane.INFORMATION_MESSAGE);
        }

        else 
        {
        JOptionPane.showMessageDialog(null, "These 5  digits are NOT a palindrome", "Results", JOptionPane.INFORMATION_MESSAGE);
        }


}

然后,main 方法需要如下所示:

public static void main(String[] args) {

    // Declare variables
    int number;
    boolean isPalindrome

    // Call method
    number = retrieveInput();
    isPalindrome = check(number);
    display(isPalindrome);


}
于 2013-09-14T01:16:58.757 回答
0

问题在于,您没有对从中获得的价值做任何事情check()。在你调用它之后main(),这个值就会消失,因为你没有将它分配给任何东西。然后在display()您创建一个新实例isPalindrome并将其设置为false. 这与中声明的无关check()。这个永远是假的,因为你没有告诉它。要修复它,请将您的main()方法的主体更改为:

// Declare variables
int number;
boolean isPalindrome;  //declare var

// Call method
number = retrieveInput();
isPalindrome = check(number);  //assign it the value
display(isPalindrome);     //pass this value on to display

然后你display

public static void display(boolean isPalindrome){

    // Declare variable
                           //don't need to decalre var as 
                           //it is now a parameter to the function
   //rest of method as before   
}

这将使您的显示器知道该数字是否是回文。

编辑如果你做错了,你也在做

if 语句后没有分号!!

结果你的'if's in check什么都不做,并且总是返回false!还要记住数字 12325 会发生什么。它不是回文,但它的第一个和最后一个数字是相同的,而第二个和第四个数字是相同的。如果它们是正确的,你的 if 语句都不会被评估为 true。以下是固定代码:

import javax.swing.JOptionPane;

public class Palindrome {
    // Main Method
    public static void main(String[] args) {
        // Declare variables
        int number;
        boolean isPalindrome; //add this var to store the value

        // Call method
        number = retrieveInput();
        isPalindrome = check(number);   //assign it so we know wheter it a palindrome or not
        display(isPalindrome);          //pass it to display() so it mknows wheter it is a palindrome or not
    }// End main method
    //*************************************************************************************
    // Method to retrieve the input

    public static int retrieveInput() {
        //Declare variables
        String number;
        int numInput = 0;
        boolean done = false;

        // Prompt user for input and validate that input
        while (!done) {
            try {
                number = JOptionPane.showInputDialog("Enter 5 single digits");
                numInput = Integer.parseInt(number);
                if (numInput < 10000 || numInput > 99999) {     //don't throw an error, inefecient just show the error instead
                    JOptionPane.showMessageDialog(null, "Entry invalid. Please re-enter 5 single digits", "Error", JOptionPane.ERROR_MESSAGE);
                } else {
                    done = true;
                }
            } catch (NumberFormatException e) {
                JOptionPane.showMessageDialog(null, "Entry invalid. Please re-enter 5 single digits", "Error", JOptionPane.ERROR_MESSAGE);
            }
        }
        return numInput;

    }// End retrieveInput method
    //*************************************************************************************
    // Method to determine if input is a palindrome or not

    public static boolean check(int number) {
        //Declare variables
        int num1,num2,num4,num5;
        boolean isPalindrome;

        num1 = number / 10000;
        num2 = number % 10000 / 1000;
        num4 = number % 100 / 10;
        num5 = number % 10;

        // Checking to see if input is a palindrome
        if (num1 == num5 && num2 == num4){  //no semicolons!!! else the if does nothing
            isPalindrome = true;            // and it evaluateds whaat it was supposed to like normal code
        }else{
            isPalindrome = false;
        }

        return isPalindrome;
    }// End check method
    //*************************************************************************************
    // Method to display results

    public static void display(boolean isPalindrome) { // no variables to declare as it now a parameter
        // Display results
        if (isPalindrome == true) {
            JOptionPane.showMessageDialog(null, "These 5  digits are a palindrome", "Results", JOptionPane.INFORMATION_MESSAGE);
        } else {
            JOptionPane.showMessageDialog(null, "These 5  digits are NOT a palindrome", "Results", JOptionPane.INFORMATION_MESSAGE);
        }
    } // End display method 
    //************************************************************************************* 
} // End class Palindrome  
于 2013-09-14T02:36:36.910 回答
0

isPalindrome应该在您的主要方法中并从您的检查方法中获取值。然后将其传递给display方法。现在的isPalindrome方式总是错误的,因为它只是false在您的display方法中初始化并且永远不会改变。

于 2013-09-14T01:16:44.207 回答