问题描述如下:
problem description
i have a very simple problem for you. given two integers a and b, your job is to calculate the sum of a + b.
参考代码如下:
public static void main(string[] args) { // todo auto-generated method stub int s; string string1 = null; string string2 = null; scanner scanner = new scanner(system.in); string1 = scanner.nextline(); system.out.println("the first number:" + string1); string2 = scanner.nextline(); system.out.println("the second number:" + string2); char a1[] = string1.tochararray(); int a[] = new int[a1.length]; for (int i = 0; i < a1.length; i++) { a[i] = integer.valueof(a1[i]).intvalue() - 48; } char b1[] = string2.tochararray(); int b[] = new int[b1.length]; for (int j = 0; j < b1.length; j++) { b[j] = integer.valueof(b1[j]).intvalue() - 48; } add(a, b); } public static void add(int c[], int d[]) { int temp = 0; int e[] = new int[50]; int c1 = c.length - 1, d1 = d.length - 1, e1 = e.length - 1; while (c1 >= 0 && d1 >= 0) { if (c[c1] + d[d1] > 9) { e[e1] = c[c1] + d[d1] - 10 + temp; temp = 1; } else { e[e1] = c[c1] + d[d1] + temp; temp=0; } c1--; d1--; e1--; } while (c1 >= 0 || d1 >= 0) { if (c1 >= 0) { e[e1] = c[c1] + temp; temp=0; } else { e[e1] = d[d1] + temp; temp=0; } c1--; d1--; e1--; } system.out.println(); string sum=arrays.tostring(e); system.out.println(sum); }
相关推荐:
java代码实现:aes加密
java实现精确的加减乘除代码
以上就是java两个整型相加的实现代码的详细内容。