如何让回文程序不识别空格并以句点结尾?
How to get palindrome program to not recognize whitespace and to end with a period?
我需要帮助我已经尝试查找许多方法,但似乎无法使它起作用。我需要它不识别白色 space,所以如果用户输入 le vel,它应该说,"Yes it is a Palindrome",就像它是水平 //没有白色 space 一样。用户输入需要以句点结尾,程序不应考虑句点。这么水平。应该 return 正确。
import java.util.Scanner;
public class PalindromeDemo
{
public static void main(String[] args)
{
String phrase, answer;
Scanner keyboard = new Scanner(System.in);
do
{
System.out.println("I will determine if a string is a palindrome");
System.out.println("Enter a word or characters and end it with a period");
phrase = keyboard.nextLine();
Palindrome pd = new Palindrome();
if(pd.checkPalindrome(phrase))
System.out.println("YES, the phrase is palindrome!");
else
System.out.println("NO, the phrase is NOT palindrome.");
System.out.println();
System.out.println("Would you like to continue? Enter yes or no");
answer = keyboard.nextLine();
System.out.println();
}
while(answer.equalsIgnoreCase("yes"));
}
}
public class Palindrome
{
public static final int MAX_CHARS = 80;
public boolean checkPalindrome(String text)
{
char[] array = new char[80];
int length = text.length();
String reverseText = "";
for(int i = length-1; i >= 0; i--)
{
reverseText = reverseText + text.charAt(i);
}
if(reverseText.equalsIgnoreCase(text))
{
return true;
}
else
{
return false;
}
}
}
要删除句点,请使用 .replace(".", "")
。要删除前导和尾随空格,请使用 .trim()
。两者都应用于 String
对象。只需在 if
语句所在的位置使用 pd.checkPalindrome(phrase.replace(".", "").trim())
即可。
此外,
if(pd.checkPalindrome(phrase))
System.out.println("YES, the phrase is palindrome!");
else
System.out.println("NO, the phrase is NOT palindrome.");
System.out.println();
System.out.println("Would you like to continue? Enter yes or no");
answer = keyboard.nextLine();
System.out.println();
应该有括号以保持逻辑流畅。另外,在 Palindrome
中,为什么你有一个 char[]
你不使用?其次,我认为方法 checkPalindrome
应该是静态的,不需要实例化。
使用这个方法:
public boolean checkPalindrome(String text) {
// remove all whitespace from input word (do this FIRST)
text = text.replaceAll("\s+", "");
// remove optional period from end of input word
if (text.endsWith(".")) {
text = text.substring(0, text.length() - 1);
}
char[] array = new char[80];
int length = text.length();
String reverseText = "";
for (int i = length-1; i >= 0; i--) {
reverseText = reverseText + text.charAt(i);
}
if (reverseText.equalsIgnoreCase(text)) {
return true;
}
else {
return false;
}
}
试试这个。输入字符串没有改变,这比上面建议的更快更有效。
public static boolean checkPalindrome(String input){
//input always contains period in the end.
//ignore it.
int length = input.length()-1-1;
int i= 0;
int j= length;
while(i<j){
if(input.charAt(i) == ' '){
i++;
} if (input.charAt(j) == ' '){
j--;
}
if(input.charAt(i) ==input.charAt(j)){
i++;
j--;
}else{
return false;
}
}
return true;
}
我需要帮助我已经尝试查找许多方法,但似乎无法使它起作用。我需要它不识别白色 space,所以如果用户输入 le vel,它应该说,"Yes it is a Palindrome",就像它是水平 //没有白色 space 一样。用户输入需要以句点结尾,程序不应考虑句点。这么水平。应该 return 正确。
import java.util.Scanner;
public class PalindromeDemo
{
public static void main(String[] args)
{
String phrase, answer;
Scanner keyboard = new Scanner(System.in);
do
{
System.out.println("I will determine if a string is a palindrome");
System.out.println("Enter a word or characters and end it with a period");
phrase = keyboard.nextLine();
Palindrome pd = new Palindrome();
if(pd.checkPalindrome(phrase))
System.out.println("YES, the phrase is palindrome!");
else
System.out.println("NO, the phrase is NOT palindrome.");
System.out.println();
System.out.println("Would you like to continue? Enter yes or no");
answer = keyboard.nextLine();
System.out.println();
}
while(answer.equalsIgnoreCase("yes"));
}
}
public class Palindrome
{
public static final int MAX_CHARS = 80;
public boolean checkPalindrome(String text)
{
char[] array = new char[80];
int length = text.length();
String reverseText = "";
for(int i = length-1; i >= 0; i--)
{
reverseText = reverseText + text.charAt(i);
}
if(reverseText.equalsIgnoreCase(text))
{
return true;
}
else
{
return false;
}
}
}
要删除句点,请使用 .replace(".", "")
。要删除前导和尾随空格,请使用 .trim()
。两者都应用于 String
对象。只需在 if
语句所在的位置使用 pd.checkPalindrome(phrase.replace(".", "").trim())
即可。
此外,
if(pd.checkPalindrome(phrase))
System.out.println("YES, the phrase is palindrome!");
else
System.out.println("NO, the phrase is NOT palindrome.");
System.out.println();
System.out.println("Would you like to continue? Enter yes or no");
answer = keyboard.nextLine();
System.out.println();
应该有括号以保持逻辑流畅。另外,在 Palindrome
中,为什么你有一个 char[]
你不使用?其次,我认为方法 checkPalindrome
应该是静态的,不需要实例化。
使用这个方法:
public boolean checkPalindrome(String text) {
// remove all whitespace from input word (do this FIRST)
text = text.replaceAll("\s+", "");
// remove optional period from end of input word
if (text.endsWith(".")) {
text = text.substring(0, text.length() - 1);
}
char[] array = new char[80];
int length = text.length();
String reverseText = "";
for (int i = length-1; i >= 0; i--) {
reverseText = reverseText + text.charAt(i);
}
if (reverseText.equalsIgnoreCase(text)) {
return true;
}
else {
return false;
}
}
试试这个。输入字符串没有改变,这比上面建议的更快更有效。
public static boolean checkPalindrome(String input){
//input always contains period in the end.
//ignore it.
int length = input.length()-1-1;
int i= 0;
int j= length;
while(i<j){
if(input.charAt(i) == ' '){
i++;
} if (input.charAt(j) == ' '){
j--;
}
if(input.charAt(i) ==input.charAt(j)){
i++;
j--;
}else{
return false;
}
}
return true;
}