回文计划没有给出正确的输出

时间:2013-09-14 01:11:37

标签: java

好的,我知道我在这里很容易丢失一些东西,但是我找不到我的错误。程序运行正常,它只返回错误的消息。我没有正确地称它为某事。我可以从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 个答案:

答案 0 :(得分:3)

main的最后两行更改为

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

display的声明更改为

public static void display(boolean isPalindrome){

并删除display中显示

的行
boolean isPalindrome = false;

这样,isPalindrome合作的display将与check中评估的if (num1 != num5 && num2 != num4); 相同。

修改

同时删除

末尾的分号
isPalindrome

否则它下面的块将在每种情况下运行,并将{{1}}设置为false。或者,您可以完全删除此条件及其下方的块,因为它实际上没有做任何事情。

答案 1 :(得分:0)

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

答案 2 :(得分: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);
        }


}

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

public static void main(String[] args) {

    // Declare variables
    int number;
    boolean isPalindrome

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


}

答案 3 :(得分: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'在检查中什么也不做,总是会返回false! 还要记住数字12325会发生什么。它不是回文,但是它的第一个和最后一个数字是相同的,而第二个和第二个数字是相同的。如果它们是正确的,那么你的if语句都不会被评估为真。以下是固定代码:

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