java任意数各个位数和_java 实现任意位数相加!

package com.ajax; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; /**  * @author xn14168  *  * TODO To change the template for this generated type comment go to  * Window - Preferences - Java - Code Style - Code Templates  */ public class Test {     public static void main(String[] args) {         while(true) {             BufferedReader br = new BufferedReader(new InputStreamReader(System.in));             String a = null;             String b = null;             try {                 System.out.println("Number1:");                 a = br.readLine();                 System.out.println("Number2:");                 b = br.readLine();                                           Object[] result = add(a, b);             System.out.println("Result : ");             for(int i=0; i < result.length; i++) {                 System.out.print(result[i]);             }             System.out.println();             System.out.println("Continue?(Y/N)");             String exit = br.readLine();             if("N".equalsIgnoreCase(exit)) {                 break;             }             } catch (IOException e) {                 e.printStackTrace();             }         }     }             private static Object[] add(String a, String b) {         String m = a;         String n = b;         if(a.length()>b.length()) {             n = padZero(b, a.length()-b.length());         } else {             m = padZero(a, b.length()-a.length());         }         System.out.println(m);         System.out.println(n);         long[] x = getNumberArray(m);         long[] y = getNumberArray(n);         return doAdd(x,y);     }         private static long[] getNumberArray(String number) {         int time = getTime(number);                 if(time == 1 ) {             return new long[]{Long.parseLong(number)};         }                 long[] result = new long[time];         String temp = number;         for(int i=time-1; i >=0; i-- ) {             if(temp.length() <=18) {                 result[i] = Long.parseLong(temp);             } else {                 String subNum = temp.substring(temp.length()-18, temp.length());                 result[i] = Long.parseLong(subNum);                 temp = temp.substring(0, temp.length()-18);             }                     }                 return result;     }         private static String padZero(String a, int zeroNum) {         StringBuffer sb = new StringBuffer();                 for(int i = 0; i=0; i--) { //            System.out.print(a[i] + " + " + b[i] + " + " + jinwei);             result[i] = new Long(a[i] + b[i] + jinwei);             long longValue = ((Long)result[i]).longValue();             if( longValue >= 1000000000000000000l) {                 jinwei = 1;                 String temp = String.valueOf(longValue - 1000000000000000000l);                 int needPad = 18 - temp.length();                 result[i] = new String(padZero(temp, needPad));             } else if(i!=0) {                 String temp = String.valueOf(longValue);                 int needPad = 18 - temp.length();                 result[i] = new String(padZero(temp, needPad));                 jinwei = 0;             } //            System.out.println("= " + result[i]);         } //        if(jinwei == 1) { //            long longValue = ((Long)result[0]).longValue(); //            result[0] =  new Long(longValue + 1l); //        }         return result;     }     }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值