“String is palindrome or not” 翻译成中文是 “字符串是否回文”。

huangapple go评论66阅读模式
英文:

String is palindrome or not

问题

我需要检查一个字符串是否是回文,但我总是得到"not palindrome"。我的错误是什么?

public static void main(String[] args) {
    String st = "iti";
    StringBuffer st1 = new StringBuffer(st);
    st1.reverse();
    System.out.println(st1);
    System.out.print(st.equals(st1));
    if (st1.equals(st) == true) {
        System.out.print("回文字符串");
    } else {
        System.out.print("不是回文");
    }
}
英文:

I need to check whether a string is a palindrome, but I'm always getting "not palindrome". What's my mistake?

public static void main(String[] args) {
    String st = "iti";
    StringBuffer st1 =new StringBuffer(st);
    st1.reverse();
    System.out.println(st1);
    System.out.print(st.equals(st1));
    if(st1.equals(st)==true) {
        System.out.print("palindrome string");
    } else {
        System.out.print("not palindrome");
    }
}

答案1

得分: 2

A StringBuffer 不是一个 String,因此它永远不会等于一个 String。请与 st1.toString() 比较而不是与 st 比较。

另外,在 if(st1.equals(st)==true) 中,与 true 比较布尔值是不必要的,容易出错。你应该将其写成 if(st1.toString().equals(st)),这样你就不会因错误使用单个 = 并引入难以注意到的错误。

class Palindrome {
    public static void main(String[] args) {
        String st = "iti";
        StringBuffer st1 = new StringBuffer(st);
        st1.reverse();
        if (st1.toString().equals(st)) { // 注意这里使用了 `toString`
            System.out.print("回文字符串");
        } else {
            System.out.print("不是回文");
        }
    }
}
英文:

A StringBuffer is not a String, thus it will never be equal to a String. Compare st with st1.toString() instead.

As an aside, in if(st1.equals(st)==true) comparing a boolean with true is not necessary and error-prone. You should write it as if(st1.toString().equals(st)) so you don't use a single = by mistake and introduce a bug that can be rather hard to notice.

class Palindrome {
	public static void main(String[] args) {
		String st = "iti";
		StringBuffer st1 =new StringBuffer(st);
		st1.reverse();
		if(st1.toString().equals(st)) { // HERE notice the use of `toString`
			System.out.print("palindrome string");
		} else {
			System.out.print("not palindrome");
		}
	}
}

答案2

得分: 0

public static void main(String[] args) {
String st = "iti";
StringBuffer st1 = new StringBuffer(st);
st1.reverse();
System.out.println(st1);
System.out.println(st.equals(st1));
if (st.equals("" + st1)) { // convert st1 from StringBuffer to String
System.out.print("回文字符串");
} else {
System.out.print("不是回文字符串");
}
}

英文:
public static void main(String[] args) {
String st = "iti";
StringBuffer st1 =new StringBuffer(st);
st1.reverse();
System.out.println(st1);
System.out.println(st.equals(st1));
if(st.equals(""+st1)) { // convert st1 from StringBuffer to String
    System.out.print("palindrome string");
} else {
    System.out.print("not palindrome");
}

}

答案3

得分: 0

To derive the String value within StringBuffer, you need to call the toString method.

System.out.print(st.equals(st1.toString()));

And, at your if-conditional statement.

if(st1.toString().equals(st)==true)
英文:

To derive the String value within StringBuffer, you need to call the toString method.

System.out.print(st.equals(st1.toString()));

And, at your if-conditional statement.

if(st1.toString().equals(st)==true)

huangapple
  • 本文由 发表于 2023年5月29日 16:12:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76355672.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定