• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

java两个整型相加的实现代码

java 搞代码 4年前 (2022-01-09) 17次浏览 已收录 0个评论

本篇文章给大家带来的内容是关于java两个整型相加的实现代码 ,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

问题描述如下:

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();<a>本文来源gao($daima.com搞@代@#码8网^</a>		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两个整型相加的实现代码的详细内容,更多请关注搞代码gaodaima其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:java两个整型相加的实现代码
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址