该程序确定用户输入的字符串是否为回文。
import acm.program.ConsoleProgram;
公共类 PurePalindrome 扩展 ConsoleProgram {
public void run() {
String originalString;
String reversedString;
boolean isPalindrome;
originalString = readLine("? ");
reversedString = reverseString(originalString);
isPalindrome = checkPalindrome(originalString, reversedString);
println("The word you entered " + determineWord(isPalindrome)
+ " a palindrome. " + originalString + " reversed is: "
+ reversedString + ".");
}
private boolean checkPalindrome(String word, String revWord) {
if (revWord.equals(word)) {
return true;
} else {
return false;
}
}
private String reverseString(String wordToReverse) {
String reversedWord = "";
for (int i = 0; i < wordToReverse.length(); i++) {
reversedWord = wordToReverse.charAt(i) + reversedWord;
}
return reversedWord;
}
private String determineWord(boolean palindrome) {
if (palindrome) {
return "is";
} else {
return "is not";
}
}
}
所有这些方法都会被认为是纯函数吗?如果不是,为什么不呢?我在确定一个方法是否是纯函数时遇到了一些麻烦。