我用Java创建了一个Palindrome程序,它接受用户输入的一个或多个单词并将它们放入一个数组中。然后它接受反向输入并将其放入另一个数组中。然后我检查两个单词是否相同。
我的问题是程序正在改变其中一个字母,这样它总是正确的并且迭代次数太多了。关于我的代码中是什么导致这个的任何想法?
public static boolean isPalindrome(char[] a, int used){
char[] original = a;
int newNumber = used - 1;
for(int i = 0; i <= newNumber; i++){
a[i] = a[newNumber - i ];
System.out.println("Your original word was: " + String.valueOf(original));
System.out.println("Backwards, your word is: " + String.valueOf(a));
}
if(String.valueOf(original).equalsIgnoreCase(String.valueOf(a))){
System.out.println("Your word or words are palindromes.");
}else{
System.out.println("Your word or words are not palindromes");
}
return(String.valueOf(original).equalsIgnoreCase(String.valueOf(a)));
}
public void userInput(){
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter a word or words that you believe are palindromes: ");
word = keyboard.next();
word = word.replaceAll("\\W","");
int length = word.length();
char[] p = new char[length];
for(int i = 0; i < length; i++){
p[i] = word.charAt(i);
}
Palindrome.isPalindrome(p, length);
}
当我输入“Candy”时,这是我的输出。
Your original word was: yandy
Backwards, your word is: yandy
Your original word was: ydndy
Backwards, your word is: ydndy
Your original word was: ydndy
Backwards, your word is: ydndy
Your original word was: ydndy
Backwards, your word is: ydndy
Your original word was: ydndy
Backwards, your word is: ydndy
Your word or words are palindromes
感谢大家的回答! 这是正确的代码,尽管效率不高:
public static boolean isPalindrome(char[] a, int used){
char[] original = a;
char[] newArray = new char[used];
int newNumber = used - 1;
for(int i = 0; i <= newNumber; i++){
newArray[i] = a[newNumber - i ];
}
System.out.println("Your original word was: " + String.valueOf(original));
System.out.println("Backwards, your word is: " + String.valueOf(newArray));
if(String.valueOf(original).equalsIgnoreCase(String.valueOf(newArray))){
System.out.println("Your word or words are palindromes.");
}else{
System.out.println("Your word or words are not palindromes");
}
return(String.valueOf(original).equalsIgnoreCase(String.valueOf(newArray)));
}
答案 0 :(得分:2)
在你的回文功能中: -
for(int i = 0; i <= newNumber; i++){
a[i] = a[newNumber - i ];
System.out.println("Your original word was: " + String.valueOf(original));
System.out.println("Backwards, your word is: " + String.valueOf(a));
}
这里你要替换现有的数组。
您甚至可以尝试以下代码: -
String original, reverse="";
Scanner in = new Scanner(System.in);
System.out.println("Enter a string to check if it is a palindrome");
original = in.nextLine();
int length = original.length();
for ( int i = length - 1 ; i >= 0 ; i-- )
reverse = reverse + original.charAt(i);
if (original.equals(reverse))
System.out.println("Entered string is a palindrome.");
else
System.out.println("Entered string is not a palindrome.");
答案 1 :(得分:0)
如果您想以这种简单的方式查看palindrome
,可以尝试使用StringBuilder
。
public static boolean isPalindrome(String input) {
StringBuilder sb=new StringBuilder(input);
return input.equals(sb.reverse().toString());
}
根据您的情况更改isPalindrome
方法,如下所示
public static void isPalindrome(char[] a, int used){
char[] newArr = new char[a.length]; // create a new array in same length as a
int newNumber = used - 1;
System.out.println("Your original word was: " + String.valueOf(a));
for(int i = 0; i <= newNumber; i++){
newArr[i] = a[newNumber - i ];
}
System.out.println("Backwards, your word is: " + String.valueOf(newArr));
if(String.valueOf(newArr).equalsIgnoreCase(String.valueOf(a))){
System.out.println("Your word or words are palindromes.");
}else{
System.out.println("Your word or words are not palindromes");
}
}
答案 2 :(得分:0)
问题在于您的代码a[i] = a[newNumber - i ];
正在更改现有数组。
首先创建一个新数组,然后将反向字符串放入其中。
public static boolean isPalindrome(char[] a, int used) {
char[] newA = new char[a.length];
int newNumber = used - 1;
for (int i = 0; i <= newNumber; i++) {
newA[i] = a[newNumber - i];
System.out.println("Your original word was: " + String.valueOf(a));
System.out.println("Backwards, your word is: " + String.valueOf(newA));
}
if (String.valueOf(String.valueOf(newA)).equalsIgnoreCase(String.valueOf(a))) {
System.out.println("Your word or words are palindromes.");
} else {
System.out.println("Your word or words are not palindromes");
}
return (String.valueOf(String.valueOf(newA)).equalsIgnoreCase(String.valueOf(a)));
}
还有许多其他有效的方法来编写此代码。
答案 3 :(得分:0)
无需复制数组(您只复制了参考文献),只创建了一个大小相同的新数组,并使用a
作为原始数组。
char[] reverse = new char[used];
将system.out
放在for循环之外:
for(int i = 0; i <= newNumber; i++){
reverse[i] = a[newNumber - i ];
}
System.out.println("Your original word was: " + String.valueOf(a));
System.out.println("Backwards, your word is: " + String.valueOf(reverse));
整个是thePalindrom功能:
public static boolean isPalindrome(char[] a, int used){
char[] reverse = new char[used];
int newNumber = used -1;
for(int i = 0; i <= newNumber; i++){
reverse[i] = a[newNumber - i ];
}
System.out.println("Your original word was: " + String.valueOf(a));
System.out.println("Backwards, your word is: " + String.valueOf(reverse));
if(String.valueOf(reverse).equalsIgnoreCase(String.valueOf(a))){
System.out.println("Your word or words are palindromes.");
}else{
System.out.println("Your word or words are not palindromes");
}
return(String.valueOf(reverse).equalsIgnoreCase(String.valueOf(a)));
}
答案 4 :(得分:0)
问题出在您分配的isPalindrome
方法
char[] original = a;
char []变量是引用。通过设置original = a
,您将复制引用,因此现在两个数组都指向同一块内存。
现在,当您修改a
时,original
也会被修改,因为它们都指向同一个内存。
您可以通过创建新实例来解决此问题,如下所示:
char[] original = new char [a.length];
System.arraycopy( a, 0, original, 0, a.length );