gpt4 book ai didi

java - 带方法的 Palindrome.java

转载 作者:行者123 更新时间:2023-12-01 23:08:00 26 4
gpt4 key购买 nike

该程序运行时不会引发任何异常,但无论输入如何,结果始终相同:““空白”是回文。”每次输入都是回文时,我只是想知道是否有人对为什么会发生这种情况有任何建议?下面是该程序的代码:

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");
}
}

最佳答案

display(boolean, int)的第二行,您正在尝试将 boolean 值与 = 进行比较运算符(operator)。这设置 digitsfalse我想你的意思是:

        if (digits == false)

if 语句需要一个 boolean 值作为输入,即 if(bool) 。您不需要像if(bool == true)那样检查这是真还是假。因为bool == true始终评估为等于 bool 。说bool == true是完全多余的。然而,你bool == false始终计算为 bool 的相反值。 !运算符翻转以下 boolean 值的值,例如!true计算结果为 false!bool因此总是评估 bool 的相反值,与 bool == false 相同.

因此,您只需要:

        if (!digits)

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

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

替换

if (digits = false)
JOptionPane.showMessageDialog(null, num + " is a palindrome");
else
JOptionPane.showMessageDialog(null, num + " is not a palindrome");

JOptionPane.showMessageDialog(null, num + " is " + (digits ? "not " : "") + "a palindrome");

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

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

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
}
}
}

关于java - 带方法的 Palindrome.java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22485508/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com