好的,我知道我在这里遗漏了一些简单的东西,但是我找不到我的错误。该程序运行良好,它只是返回错误消息。我没有正确称呼它或其他什么。我可以从 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