英文:
How to setup a y/n loop in java?
问题
以下是已经翻译好的部分:
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"));
}
英文:
I have a program that is a palindrome checker that takes an input reverses it and checks if the original input is equal to the reverse. I'm trying to get a y/n loop inside of the program to check if the user wants to enter another palindrome. This is my code:
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"));
}
This is the output I get:
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
I can't figure out how to get it to wait for another entry and test that one instead. Any help would be great.
答案1
得分: -1
好的,我稍微修改了你的代码,实际上你的代码已经很接近正确了。以下是你的代码的翻译部分:
public static void main(String[] args) {
String checker = "y"; // 设置 checker 以确保第一个循环
while(checker.equalsIgnoreCase("y")) { // 检查正确的短语
System.out.println("输入要检查的字符串!"); // 初始消息
Scanner reader = new Scanner(System.in); // 读取第一行
String input = reader.nextLine();
if (isPalindrome(input)) { // 检查是否为回文
System.out.println(input + " 是回文!");
} else {
System.out.println(input + " 不是回文!");
}
System.out.println("是否要输入另一个字符串? y/n");
checker = reader.nextLine(); // 将 checker 设置为下一个循环的响应
}
System.out.println("好的,再见 :)"); // 结束消息
}
private static boolean isPalindrome(String s){
String reverse = ""; // 创建用于后续比较的字符串
for (int i = 0; i < s.length(); i++) {
reverse += s.trim().charAt(s.length() - i - 1); // 反转字符串
}
return reverse.equalsIgnoreCase(s); // 返回不区分大小写的布尔值
}
我真的没有做太多改动:
- 我将检查回文的部分放在了一个函数中,以提高可读性。
- 在处理用户输入和一般文本时,使用字符串(Strings)通常是更好的做法。
英文:
Ok i reworked your code a bit and tbh you werent far off.
Here is a pastebin link with my code.
public static void main(String[] args) {
String checker = "y"; //set checker to ensure the first cycle
while(checker.equalsIgnoreCase("y")) { // check for correct phrase
System.out.println("Enter a string to check!"); // inital message
Scanner reader = new Scanner(System.in); // read first line
String input = reader.nextLine();
if (isPalindrome(input)) { // check input for palindrome
System.out.println(input + " is a Palindrome!");
} else {
System.out.println(input + " is not a Palindrome!");
}
System.out.println("Do you want to enter another string? y/n");
checker = reader.nextLine(); // set checker to response for next cycle
}
System.out.println("Ok bye :)"); // death message
}
private static boolean isPalindrome(String s){
String reverse = ""; // create string for later comparison
for (int i = 0; i < s.length(); i++) {
reverse+=s.trim().charAt(s.length()-i-1); // reverse the string
}
return reverse.equalsIgnoreCase(s); // return boolean ignoring case
}
I really didnt change that much:
- I check the palindrome in a function for better readablility
- Used Strings instead of char[]. Its generally better practice handling userinput and general text that way.
EDIT:
my output:
Enter a string to check!
hey
hey is not a Palindrome!
Do you want to enter another string? y/n
y
Enter a string to check!
brb
brb is a Palindrome!
Do you want to enter another string? y/n
n
Ok bye :)
Process finished with exit code 0
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论