This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center。
7年前关闭。
无论我输入什么字符串,这总是打印“这不是回文”
7年前关闭。
public class reverse
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a new string : ");
String word = sc.nextLine();
StringBuilder s = new StringBuilder();
for (int i = 0; i <= word.length() - 1; i++)
{
char c = word.charAt(word.length() - i - 1);
s.append(c);
}
System.out.println("Reversed String = " + s);
if (word.equals(s))
{
System.out.println("This is a palindrome");
}
else
{
System.out.println("This is not a palindrome");
}
}
}
无论我输入什么字符串,这总是打印“这不是回文”
最佳答案
你需要
if (word.equals(s.toString())){
...
}