我正在尝试调试程序,使其包含潜在的回文句。现在,它仅告诉我反向的单个单词是回文,但是它总是告诉我句子不是回文,即使它是回文。
我曾尝试将original.equals(reverse)更改为reverse.equals(original),但我继续获得相同的输出。
import java.util.Scanner;
public class WordPlay {
public static void main(String [] args) {
// Variable declarations
String original, reverse = "";
boolean lipE = true;
boolean lipS = true;
boolean palindrome = true;
boolean alliteration = true;
// Scanner object
Scanner input = new Scanner(System.in);
System.out.print("Please enter a sentence: ");
original = input.nextLine();
original = original.toLowerCase();
System.out.println("The input that you entered was: " + original);
// Determine whether a input is a lipogram
for(int i = 0; i < original.length(); i++)
if(original.charAt(i) == 'e' || original.charAt(i) == 'E' )
lipE = false;
if(lipE) {
System.out.println("The input is a lipogram. ");
}
else {
System.out.println("The input is not a lipogram avoiding the
letter E.");
}
// Determine whether a input is a lipogram
for(int i = 0; i < original.length(); i++)
if(original.charAt(i) == 's' || original.charAt(i) == 'S' )
lipS = false;
if(lipS) {
System.out.println("The input is a lipogram. ");
}
else {
System.out.println("The input is not a lipogram avoiding the letter S.");
}
// Determine whether or not an input is a palindrome.
// Compare the original input to the reversed input.
double length = original.length();
for (int i = original.length() - 1; i >= 0; i--)
reverse = reverse + original.charAt(i);
if (original.equals(reverse))
System.out.println("The input is a palindrome.");
else
System.out.println("The input isn't a palindrome.");
预期结果是程序将识别出当句子被翻转并等于相同内容时,它将打印“输入是回文”。