首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Palindrome.java与方法

Palindrome.java与方法
EN

Stack Overflow用户
提问于 2014-03-18 16:34:56
回答 2查看 1K关注 0票数 0

这个程序运行时没有抛出任何异常,但是不管输入结果总是一样的:“”空白“是回文。”每次输入都是回文,我只是想知道是否有人对为什么会发生这种情况有任何建议?以下是该程序的代码:

代码语言:javascript
复制
class Palindrome
{
    public static void main(String args[])
    {
        int num = 0;
        int dig1 = 0;
        int dig2 = 0;
        int dig3 = 0;
        int dig4 = 0;
        int dig5 = 0;
        boolean digits = true;

        retrieveInput(num);
        check(num,dig1,dig2,dig3,dig4,dig5);
        display(digits,num);
    }

    public static int retrieveInput(int num)
    {
        String number;
        number = JOptionPane.showInputDialog("Enter A Five Digit Number.");
        num = Integer.parseInt(number);

        while(num < 9999 || num > 99999)
        {
            JOptionPane.showMessageDialog(null, num + " Is Not A Five Digit Number",
                "ERROR", JOptionPane.ERROR_MESSAGE);        
            number = JOptionPane.showInputDialog("Enter A Five Digit Number.");
            num = Integer.parseInt(number);

        }
        return num;
    }

    public static boolean check(int num,int dig1,int dig2,int dig3,int dig4,int dig5)
    {
        boolean digits;

        dig1 = num / 10000;
        dig2 = num % 10000 / 1000;
        dig3 = num % 10000 % 1000 / 100;
        dig4 = num % 10000 % 1000 % 100 /10;
        dig5 = num % 10000 % 1000 % 100 % 10 / 1;

        if (dig1 == dig5 && dig2 == dig4)
            digits = true;
        else
            digits = false;

        return digits;
    }

    public static void display(boolean digits, int num)
    {
        num = retrieveInput(num);
        if (digits = false)
            JOptionPane.showMessageDialog(null, num + " is a palindrome");
        else
            JOptionPane.showMessageDialog(null, num + " is not a palindrome");
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-03-18 16:42:48

display(boolean, int)的第二行中,您试图将布尔值与=运算符进行比较。这将digits设置为false,我认为您的意思是:

代码语言:javascript
复制
        if (digits == false)

if语句需要布尔值作为输入,即if(bool)。您不需要检查这是真还是假,就像if(bool == true)一样,因为bool == true的计算结果总是等于bool。说bool == true是完全多余的。但是,bool == false的计算结果总是与bool相反。!运算符翻转以下布尔值,例如,!true值为false。因此,!bool的计算结果总是与bool相反,并且与bool == false相同。

因此,您只需要:

代码语言:javascript
复制
        if (!digits)

这样做也被认为是良好的做法。

另一种使代码更短的方法是使用三元操作符。这不一定是最好的做法,但我个人喜欢这样做:

替换

代码语言:javascript
复制
if (digits = false)
    JOptionPane.showMessageDialog(null, num + " is a palindrome");
else
    JOptionPane.showMessageDialog(null, num + " is not a palindrome");

使用

代码语言:javascript
复制
JOptionPane.showMessageDialog(null, num + " is " + (digits ? "not " : "") + "a palindrome");

有关三元运算符的更多信息,请参见以下页面:

另外要注意的是,您不需要将输入作为整数处理,因为您从不在算术语句中使用数字;您只需要比较数字的数字和其他数字。在这个工作代码中,我将它作为一个字符串处理,因此它要短得多:

代码语言:javascript
复制
import javax.swing.*;

class Palindrome
{
    public static void main(String args[])
    {
        display();
    }

    /**
     * Returns a five-digit string
     */
    public static String retrieveInput()
    {
        String number = JOptionPane.showInputDialog("Enter A Five Digit Number:"); //get input
        while(number.length() != 5) //continue to get input while it is not five digits
        {
            JOptionPane.showMessageDialog(null, number + " Is Not A Five Digit Number",
                "ERROR", JOptionPane.ERROR_MESSAGE);        
            number = JOptionPane.showInputDialog("Enter A Five Digit Number:");
        }
        return number; //return the input
    }

    /**
     * Returns whether the given five digit string is a palindrome
     */
    public static boolean check(String number)
    {
        return (number.charAt(0) == number.charAt(4) && number.charAt(1) == number.charAt(3)); //checks to see if the first character equals the fifth character and the second character equals the fourth character
    }

    public static void display()
    {
        String number = retrieveInput(); //gets input

        if(check(number)) //if it is a palindrome
        {
            JOptionPane.showMessageDialog(null, number + " is a palindrome"); //say it is a palindrome
        }
        else
        {
            JOptionPane.showMessageDialog(null, number + " is not a palindrome"); //say it isn't a palindrome
        }
    }
}
票数 2
EN

Stack Overflow用户

发布于 2014-03-18 17:07:58

您的代码有几个缺陷:

代码语言:javascript
复制
/* The goal of this method is to receive user input and return the
 * entered value. For this you don't need the num input parameter.
 */
public static int retrieveInput(int num)
{
    String number;
    number = JOptionPane.showInputDialog("Enter A Five Digit Number.");
    num = Integer.parseInt(number);

    while(num < 9999 || num > 99999)
    {
        JOptionPane.showMessageDialog(null, num + " Is Not A Five Digit Number",
            "ERROR", JOptionPane.ERROR_MESSAGE);        
        number = JOptionPane.showInputDialog("Enter A Five Digit Number.");
        num = Integer.parseInt(number);

    }
    return num;
}

/* Check takes a number and returns if the number is a palindrome or not.
 * The digits of the number are calculated within the method and thus 
 * shouldn't be parameters. (You didn't use these anyway)
 */
public static boolean check(int num,int dig1,int dig2,int dig3,int dig4,int dig5)
{
    boolean digits;

    /* If you removed the paremeters, you've to declare the variables here,
     * e.g. int dig1 = num / 10000
     */
    dig1 = num / 10000;
    dig2 = num % 10000 / 1000;
    dig3 = num % 10000 % 1000 / 100; // This is not needed, because it's not used anywhere
    dig4 = num % 10000 % 1000 % 100 /10;
    dig5 = num % 10000 % 1000 % 100 % 10 / 1;

    /* The pattern you used here is basically
     * if cond == true then return true else return false
     * You never have to use this pattern, since you can
     * directly return conditions
     */
    if (dig1 == dig5 && dig2 == dig4)
        digits = true;
    else
        digits = false;

    return digits;
}

/*
 * This method displays a popup to the user that states if a given number
 * is a palindrome or not. It expects the result and the number as paremeters.
 */
public static void display(boolean digits, int num)
{
    /* You are (again) retrieving the input here, although the input
     * is passed to this method.
     */
    num = retrieveInput(num);

    /* As The Guy with The Hat pointed out in his answer,
     * = is the assignment operator. For comparisons you have to use
     * == instead.
     */
    if (digits = false)
        JOptionPane.showMessageDialog(null, num + " is a palindrome");
    else
        JOptionPane.showMessageDialog(null, num + " is not a palindrome");
}

/*
 * Here is the general control flow of your program
 */
public static void main(String args[])
{
    // These variable declarations can be removed, since you aren't using,
    // nor need them.
    int num = 0;
    int dig1 = 0;
    int dig2 = 0;
    int dig3 = 0;
    int dig4 = 0;
    int dig5 = 0;
    boolean digits = true;

    retrieveInput(num); // you are retrieving the input, but not storing it
    check(num,dig1,dig2,dig3,dig4,dig5); // you are checking, but not storing the result
    display(digits,num);
}

更正版本:

代码语言:javascript
复制
public static void main(String args[]) {
    int num = retrieveInput();
    boolean isPal = check(num);
    display(num, isPal);
}

public static int retrieveInput() {
    String number = JOptionPane
            .showInputDialog("Enter A Five Digit Number.");
    int num = Integer.parseInt(number);

    while (num < 9999 || num > 99999) {
        JOptionPane.showMessageDialog(null, num
                + " Is Not A Five Digit Number", "ERROR",
                JOptionPane.ERROR_MESSAGE);
        number = JOptionPane.showInputDialog("Enter A Five Digit Number.");

    }
    return num;
}

public static boolean check(int num) {
    int dig1 = num / 10000;
    int dig2 = num % 10000 / 1000;
    // int dig3 = num % 10000 % 1000 / 100;
    int dig4 = num % 10000 % 1000 % 100 / 10;
    int dig5 = num % 10000 % 1000 % 100 % 10 / 1;

    return dig1 == dig5 && dig2 == dig4
}

public static void display(int num, boolean isPal) {
    if (isPal)
        JOptionPane.showMessageDialog(null, num + " is a palindrome");
    else
        JOptionPane.showMessageDialog(null, num + " is not a palindrome");
}

请注意,我基本上没有改变你的程序的核心。在接下来的步骤中,您可能希望检查用户是否输入了非数字字符或允许n长度数字。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22485508

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档