我开发了一个程序,用于检查用户输入的字符串是否为回文序列。那部分工作正常,但我想让程序再次启动,如果用户想要输入另一个字符串。我已经查看了不同的论坛并尝试了不同的循环,但我无法让程序正确重复。我目前有一个do语句,我认为问题与调用main()
有关,因为它在netbeans
中被标记。我不知道为什么。任何帮助或推动正确的方向将不胜感激。
/*
* This program checks to see if the user input is a palindrome.
* white space and non alphanumeric characters will be ignored.
*/
package firstsubroutines;
/**
*
* @author anonymous
*/
public class FirstSubroutines {
static String str; // global variable
static String reversed; // global variable
/*
* The subroutine strp accepts a string as an argument
* and returns a string stripped of spaces and
* non alphanumeric characters
*/
static String strip(String str){
str = str.replaceAll("[^a-zA-Z0-9]", "");
str = str.toLowerCase();
System.out.println("Stripped: " + str);
return str;
} // end of subroutine stripped
/*
* The subroutine reverse accepts a string as an argument
* and returns a string in reverse order
*/
static String reverse(String str){
int i;
reversed = "";
for (i = str.length() - 1; i >= 0; i--) {
reversed = reversed + str.charAt(i);
}
System.out.println("Reversed: " + reversed);
return reversed;
} // end of subroutine reversed
/*
* This is the main progam where the subroutines
* will be called
*/
public static void main(String[] args) {
String userInput; // The input by the user.
System.out.println("This program checks to see if the user's input is a palindrome.");
System.out.println("White space and non alphanumeric characters will be ignored.");
System.out.println();
System.out.print("Please enter a string: ");
userInput = TextIO.getln(); // assigns the user input to a variable
// subroutine strip is called and an the value of
// the variable userInput is passed
str = strip(userInput);
// subroutine reverse is called and an the value of
// the variable str is passed
String rev = reverse(str);
// compares the two objects
if ( str.equals(rev) ) {
System.out.println("This IS a palindrome");
}
else {
System.out.println("This NOT a palindrome");
} // end of if statement
boolean toContinue; // True if user wants to play again.
do {
main();
System.out.print("Do you want enter another string?: ");
toContinue = TextIO.getlnBoolean();
} while (toContinue == true);
} // end main
} // end class
答案 0 :(得分:3)
将输入处理逻辑与另一种方法分开:
private static void processString(){
String userInput; // The input by the user.
System.out.println("This program checks to see if the user's input is a palindrome.");
System.out.println("White space and non alphanumeric characters will be ignored.");
System.out.println();
System.out.print("Please enter a string: ");
userInput = TextIO.getln(); // assigns the user input to a variable
// subroutine strip is called and an the value of
// the variable userInput is passed
str = strip(userInput);
// subroutine reverse is called and an the value of
// the variable str is passed
String rev = reverse(str);
// compares the two objects
if ( str.equals(rev) ) {
System.out.println("This IS a palindrome");
}
else {
System.out.println("This NOT a palindrome");
} // end of if statement
}
然后只需在循环中从main
调用它:
public static void main(String[] args) {
boolean toContinue = false; // True if user wants to play again.
do {
processString();
System.out.print("Do you want enter another string?: ");
toContinue = TextIO.getlnBoolean();
}
while (toContinue == true);
}
答案 1 :(得分:0)
使用Scanner
类:
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String input = sc.next();
// do something with input
}