我的教授评估了我的作业并说它需要进行称为“功能分解”的操作。这是什么意思,以及在我检查回文程序时它应该是什么样子?
import java.util.Scanner;
public class Palindrome {
public static void main(String[] args) {
String word;
/**
* create scanner for input and ask user for phrase/word
*/
Scanner kb = new Scanner(System.in);
System.out.println("Enter a word to see if its a palindrome.");
word = kb.nextLine();
/**
* this just removes spaces. For a palindrome like "race car"
*/
String newWord = word.replace(" ", "");
/**
* this removes commas like in given lab examples
*/
String newWord1 = newWord.replace(",", "");
System.out.println(isPalindrome(newWord1));
}
/**
*
* @param word
* @return true or false
*/
public static boolean isPalindrome(String word) {
/**
* if the word is 1 or 2 characters long its automatically a palindrome
*/
if(word.length() == 0 || word.length() == 1) {
return true;
}
/**
* use recursion to keep checking the first and last characters of each substring until result
*/
if (word.charAt(0) == word.charAt(word.length() - 1)) {
return isPalindrome(word.substring(1, word.length() - 1));
}
return false;
}
}
顺便说一下,他说我需要两个Java文件。如果我只是要创建一个方法,第二个Java文件会是什么呢?像制作类、构造函数、继承等我通常使用单独的文件。对于这样一个小问题,我肯定不需要制作两个文件吧?我无论如何已经在电子邮件中问过他,但如果你不知道也没关系。谢谢你抽出时间。
WordChecker
(单词输入和调用验证器)和PalindromValidator
,但如果在未来你需要ContractionValidator
呢? - kendavidson