代码如下:
      byte[] b = new byte[20];
        System.in.read(b);
        String str = new String(b).trim();     if(str == "XYZ") {
            System.out.println("Good");
        }
    else {
         System.out.println("Bad");
     };此时我在控制台输入XYZ,但为什么执行的是else中的代码?
  

解决方案 »

  1.   

     System.in.read(b);你知道这个方法是干嘛使的么从控制台读取数据不是这么读的你可以在if之前System.out.println(str );
      

  2.   

    比较相等用equals,而不是用“==”。

    if(str.equals("XYZ")) {
      System.out.println("Good");
    }P.S :楼主可以去研究下equals和==的区别。
      

  3.   


    if("XYZ".equals(str)) 
      

  4.   

    String str = new String(b).trim().intern();就OK,之前把你的trim看成intern了艹蛋
      

  5.   


            byte[] b = new byte[20];
    try {
    System.in.read(b);
    } catch (IOException e) {
    e.printStackTrace();
    }

    String str = new String(b).trim();
    //System.out.println(str); if (str.equals("XYZ")) {
    System.out.println("Good");
    } else {
    System.out.println("Bad");
    }
      

  6.   

    为什么我输入XYZ 打印的是Good?
      

  7.   


    public static void main(String[] args) throws IOException {
    // TODO Auto-generated method stub
    byte[] inBytes = new byte[3];
    System.in.read(inBytes);
    String content = new String(inBytes); System.out.println(("XYZ".equals(content))); }
      

  8.   

    如果用.trim() byte数组长度可以大于3