我正在尝试获取字符串的回文,下面是我的代码。问题是,它仅执行else条件

问题描述 投票:0回答:1

公共类回文{

public static void main(String[] args) {

    String name = "aba";

    StringBuilder sb = new StringBuilder();

    sb.append(name);

    sb = sb.reverse();
    System.out.println(sb);
    System.out.println(name);
    if (sb.equals(name)) {
        System.out.println(name + " is palindrome");
    } else System.out.println(name + " isn't palindrome");
}

}else条件仅执行。

java palindrome
1个回答
0
投票

您需要使用sb.toString()

if(sb.toString().equals(name)) ...
© www.soinside.com 2019 - 2024. All rights reserved.