这是我的第一次提交,我已经完成了四分之一的Java编程。
我有一个创建Palindrome Checker的任务。相当直接,我在第一个小时就找到了那部分代码。但是,以典型的方式(对我而言)我希望我的代码能够做得更多。这是我遇到问题的地方。
我希望此代码执行以下操作: 接受用户输入 无论情况或标点符号,都能正确识别输入是否为回文 在循环中运行,以便可以执行多个测试
到目前为止它的确有效,我只是不知道我是否采用了正确的方法。有人愿意看看,让我知道这是多么低效,或者是否有任何明显的新手错误?非常感谢。
代码:
/**
* Created by Travis on 1/10/2015.
*/
import java.io.IOException;
import java.util.*;
import javax.swing.*;
import java.text.*;
import java.lang.StringBuilder;
public class Palindrome
{
public static void main(String[] args) throws IOException //main class
{
String str = "", answer = "", test1 = "yes", test2 = "no"; //strings
int len = 10; //initial value for len so it doesn't trip the success if
Scanner KB = new Scanner(System.in); //user input
System.out.println("Greetings, Welcome to the Palindrome Checker.\n" + //initial greeting
"Would you like to check a Palindrome? (Yes or no)");
answer = KB.nextLine(); //input is for the sentinel program.
if (!(answer.equalsIgnoreCase(test1) || answer.equalsIgnoreCase(test2))) //error message in case user inputs incorrect string.
{
System.out.println("Error! You can only choose 'yes' or 'no'. Please try again:");
answer = KB.nextLine(); //allows for new answer
}
System.out.println(answer + " y"); //debugging so i can see answer
while (answer.equalsIgnoreCase(test1)) //compares to test1 which is: yes. as long as answer equals yes, the program should loop
{
System.out.println("Please provide a word or phrase:");
str = KB.nextLine(); //prompt for palindrome
String str2 = str.toLowerCase().replaceAll("\\s+", "").replaceAll("\\W+", ""); //converts input to a string that is a single group of chars with no space or punctuation
System.out.println(str2); //debug
StringBuilder str1 = new StringBuilder(str2); //takes the string and makes a stringbuilder so i can delete chars to test
len = str1.length(); //sets for length. This lets me account for any length of phrase
System.out.println(len); //debug
System.out.println(str1); //debug
for (int i = 0; len >= 2; i++) //for loop to progressively test front and back letters and proceed if they are the same
{
char ch1 = str1.charAt(0); //takes the letter at [0] and converts to char
char ch2 = str1.charAt(len - 1); //takes the last letter and converts to char
System.out.println("The first letter in your phrase is: " + ch1); // lists the letter at [0]
System.out.println("The last letter in your phrase is: " + ch2); //lists the letter at the end
if (!(ch1 == ch2)) //if the front and back letter do not match, fails and prompts for new phrase
{
System.out.println("Sorry, this phrase is not a palindrome. \n" +
"Would you like to try again?");
answer = KB.nextLine();
break;
}
else if (ch1 == ch2) //if front and back do match, removes front and back letters and updates stringbuilder
{
System.out.println("Removing letters on each end of the phrase and performing " +
"new check:\n");
str1.deleteCharAt(len - 1); //deletes the last letter from str1
str1.deleteCharAt(0); //deletes first letter from str1
len = str1.length(); //updates len with the new length of str1
i++;
}
}
if (len <= 1) //if the for loop successfully reduces stringbuilder to 1 or less characters, prompts for success
{
System.out.println("Congratulations, your phrase: '" + str + "' is a palindrome! \n\n" +
"Would you like to try again?");
answer = KB.nextLine();
len = 10; //resets len
}
}
if (answer.equalsIgnoreCase(test2)) //ends sentinel loop.
{
System.out.println(answer + " N");
System.exit(0);
}
}
}