如何在java中设置y/n循环?
我有一个程序,它是一个回文检查器,它接受一个输入,将其反转,并检查原始输入是否等于反转。我正试图在程序中得到一个y/n循环,以检查用户是否想要输入另一个回文。这是我的代码:
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
String controller = "y";
do {
System.out.println("Enter a palindrome!");
String input = scan.nextLine();
String original = input;
input = input.replaceAll("\\s", "");
int len = input.length();
char[] charArray = new char[len];
char[] charArray2 = new char[len];
for(int i = 0; i < len; i++){
charArray2[i] = input.charAt(i);
}
for(int j = 0; j < len; j++){
charArray[j] = charArray2[len-1-j];
}
String palindrome = new String(charArray);
if(palindrome.equals(input)){
System.out.println(palindrome);
System.out.println(original + " is a palindrome!\nWould you like to test another palindrome? (y/n)");
}else{
System.out.println(palindrome);
System.out.println(original + " is not a palindrome!\nWould you like to test another palindrome? (y/n)");
}
scan.next(controller);
}while(controller.equalsIgnoreCase("y"));
}
这是我得到的输出:
Enter a palindrome!
was it a cat i saw
wasitacatisaw
was it a cat i saw is a palindrome!
Would you like to test another palindrome? (y/n)
y
Enter a palindrome!
is a palindrome!
Would you like to test another palindrome? (y/n)
y
Enter a palindrome!
is a palindrome!
Would you like to test another palindrome? (y/n)
y
Enter a palindrome!
is a palindrome!
Would you like to test another palindrome? (y/n)
y
Enter a palindrome!
is a palindrome!
Would you like to test another palindrome? (y/n)
y
Enter a palindrome!
is a palindrome!
Would you like to test another palindrome? (y/n)
y
我不知道如何让它等待另一个条目并测试该条目。任何帮助都会很好
# 1 楼答案
好的,我重新修改了你的代码,你离我不远了。 Here is a pastebin link使用我的代码
我真的没有改变那么多:
编辑: 我的输出: