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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 孔肖 中级黑马   /  2012-9-2 11:44  /  1537 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 孔肖 于 2012-9-2 17:11 编辑

那天在论坛里看见一道题,查了下文档没搞明白。谁能帮我详细解释下 BigInteger,还有它的add方法。谢谢
public  class  Test
{
     public static void main(String[] args)
     {
           BigInteger one = new BigInteger("1");
           BigInteger two = new BigInteger("2");
           BgInteger three = new BigInteger("3");
           BigInteger sum = new BigInteger("0");
  
          sum.add(one);
          sum.add(two);
          sum.add(three);
  
         System.out.println(sum.toString());   //  返回结果为  0  
   }
}

2 个回复

倒序浏览
  1. import java.math.BigInteger;


  2. public class Test {

  3.         /**
  4.          * @param args
  5.          */
  6.         public static void main(String[] args) {
  7.                 // TODO Auto-generated method stub
  8.                 BigInteger one = new BigInteger("1");
  9.                 BigInteger two = new BigInteger("2");
  10.                 BigInteger three = new BigInteger("3");
  11.                 BigInteger sum = new BigInteger("0");
  12.                
  13.                 sum = sum.add(one);
  14.                 sum = sum.add(two);
  15.                 sum = sum.add(three);
  16.                
  17.                 System.out.println(sum.toString());
  18.                
  19.                
  20.         }
  21.        

  22. }
复制代码
哥们,你应该改成这样,你没把返回的值赋给sum啊
回复 使用道具 举报
add方法只是返回自己的值和传入参数的值相加,但是你没有赋值给sum;
你这样写
sum = sum.add(one);
sum = sum.add(two);
sum = sum.add(three);
System.out.println(sum.toString());   
或者这样:
System.out.println(sum.add(one).add(two).add(three));
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马