弦反転ツールを自作する練習中です。以上の学習プロセスを簡単なアプリケーションで使用しました。しかし..
Eclipseでコードを実行すると、単語が= racecarであり、逆になっている場合でも、= racecar (単語が逆)になります。else が表示されます.. word と reverse は決して同じではないことを教えてくれます..しかし..その場合は同じですか? 右?アドバイスをください、これは非常に残念です。ありがとうございました。
import java.util.Scanner;
public class Main {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args){
System.out.println("Please enter a word, I'll show it reversed ");
System.out.println("and tell you if it is read the same forward and backward(A Palindrome)!: ");
String word = sc.nextLine();
String reverse = reverse(word);
if (reverse == word){
System.out.println("Your word: " + word + " backwards is " + reverse + ".");
System.out.println("Your word is read the same forward and backward!");
System.out.println("Its a Palindrome!");
}
else {
System.out.println("Your word: " + word + " backwards is " + reverse + ".");
System.out.println("Your word is not read the same forward and backward!");
System.out.println("Its not a Palindrome!");
}
}
public static String reverse(String source){
if (source == null || source.isEmpty()){
return source;
}
String reverse = "";
for(int i = source.length() -1; i >= 0; i--){
reverse = reverse + source.charAt(i);
}
return reverse;
}
}