>  기사  >  Java  >  두 개의 정수를 추가하기 위한 Java 구현 코드

두 개의 정수를 추가하기 위한 Java 구현 코드

不言
不言원래의
2018-09-12 15:27:202106검색

이 기사의 내용은 Java에서 두 개의 정수를 추가하는 구현 코드에 관한 것입니다. 이는 특정 참조 값을 가지고 있으므로 도움이 될 수 있습니다.

문제 설명은 다음과 같습니다.

Problem Description

두 개의 정수 A와 B가 주어지면 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 구현 코드의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.