A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 够了没有 中级黑马   /  2013-11-22 10:31  /  1436 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

对于老师讲的这个键盘录入的程序,我把'\r'和'\n'变成字符串后为什么就不行?
import java.io.*;
class ReadIn {
        public static void main(String[] args) throws IOException {
                InputStream in = System.in;
                StringBuilder sb = new StringBuilder();

//以下代码块将做改动
                while(true) {
                        int ch = in.read();
                        if (ch=='\r')
                                continue;
                        if(ch=='\n') {
                                String s = sb.toString();
                                if("over".equals(s))
                                        break;
                                System.out.println(s.toUpperCase());
                        sb.delete(0,sb.length());
                        }
                        else
                                sb.append((char)ch);
                }
        }
}
-----------------------------------------------------------------------------------------------------------------------------
import java.io.*;
class ReadIn  {
        public static void main(String[] args) throws IOException {
                InputStream in = System.in;
                StringBuilder sb = new StringBuilder();

//以下代码块是做改动后的
                char[] c = {'\r'};
                String str = new String(c);
                char[] c1 = {'\n'};
                String str1 = new String(c1);
                while(true) {
                        int ch = in.read();
                        if (str.equals(ch))
                                continue;
                        if(str1.equals(ch)) {
                                String s = sb.toString();
                                if("over".equals(s))
                                        break;
                                System.out.println(s.toUpperCase());
                        sb.delete(0,sb.length());
                        }
                        else
                                sb.append((char)ch);
                }
        }
}

2 个回复

倒序浏览
这是equals()方法的源码:

public boolean equals(Object anObject) {
        if (this == anObject) {
            return true;
        }
        if (anObject instanceof String) {
            String anotherString = (String) anObject;
            int n = value.length;
            if (n == anotherString.value.length) {
                char v1[] = value;
                char v2[] = anotherString.value;
                int i = 0;
                while (n-- != 0) {
                    if (v1 != v2)
                            return false;
                    i++;
                }
                return true;
            }
        }
        return false;
    }



read()的是char类型的,if (anObject instanceof String)比较不String类型 ,他就不会继续判断了直接跳到最后的
return false;
至于为什么他们两个为什么可以使用equals()而不报错,我认为应该是char对应的int会自动装箱变为Integer类型。还请各位大侠说说。
回复 使用道具 举报
看看源码就知道了ublic boolean equals(Object anObject) {
        if (this == anObject) {
            return true;
        }
        if (anObject instanceof String) {
            String anotherString = (String) anObject;
            int n = value.length;
            if (n == anotherString.value.length) {
                char v1[] = value;
                char v2[] = anotherString.value;
                int i = 0;
                while (n-- != 0) {
                    if (v1 != v2)
                            return false;
                    i++;
                }
                return true;
            }
        }
        return false;
    }
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马