int m=o;
String s1=n1.getText;
if(s1=="liu"){m=160;}
else m=40;
n2.setText(String.valueOf(m));当输入liu时,为啥输出的值为40??

解决方案 »

  1.   

    改一下:
    ......
    if(s1.equals("liu")){m = 160;}
    else m = 40;
    ......
    就好了
      

  2.   

    这个问题多看API就会懂的,关于equals的String解释:
    英文:
    equalspublic boolean equals(Object anObject)
    Compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.
    Overrides:
    equals in class Object
    Parameters:
    anObject - The object to compare this String against
    Returns:
    true if the given object represents a String equivalent to this string, false otherwise
    中文意思:
    public boolean equals(Object anObject)
    比较此字符串与指定的对象。当且仅当该参数不为 null,并且是表示与此对象相同的字符序列的 String 对象时,结果才为 true。
    覆盖:
    类 Object 中的 equals
    参数:
    anObject - 与此 String 进行比较的对象。
    返回:
    如果 String 相等,则返回 true;否则返回 false。
    意思就是说,String里,equals比较的是字符串内容是否相等,而==比较的是内存。。
    所以用equals在这里才是正确的