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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 吴亨 黑马帝   /  2011-12-15 22:13  /  1307 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

为何输入0时,输出-35?
import java.util.*;
public class Projection1 {

    public static void main(String[] args) {

           
            Vector v = new Vector();
            int sum = 0;
            int num = 0;
            while(true)
            {
            try
            {

                num = System.in.read();
            }
            catch(Exception ex){ex.printStackTrace();}
                if(num == '\n')
                            break;
                else

             num = num-'0';
                     v.addElement(new Integer(num));

            }
            Enumeration e = v.elements();
            while(e.hasMoreElements())
            {
              Integer intObj = (Integer)e.nextElement();
              sum+=intObj.intValue();
            }
            System.out.println(sum);

    }
}

2 个回复

倒序浏览
王冀 黑马帝 2011-12-15 22:39:01
沙发
因为当在命令行中按回车时输入了两个字符"\r\n"
而"\r"的ascii码为13
13-48=-35

评分

参与人数 1技术分 +1 收起 理由
王德云 + 1

查看全部评分

回复 使用道具 举报
吴亨 黑马帝 2011-12-16 12:49:04
藤椅
王冀 发表于 2011-12-15 22:39
因为当在命令行中按回车时输入了两个字符"\r\n"
而"\r"的ascii码为13
13-48=-35

嗯。高手。加我qq:1023517092,交流一下行吗?
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马