java--整数翻转后相加

2014 华为校园招聘机试题-------编程:

题目:给定2个整数,将其翻转后相加,输出结果。如“786”和“5412”,则为结果为:687+2145=2832

import java.util.Scanner;

public class intRevPlus {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		String val1 = sc.next();
		String val2 = sc.next();
		// System.out.println(val+"----"+val2);
		val1 = reverse(new StringBuffer(val1));
		val2 = reverse(new StringBuffer(val2));
		try {
			int result = Integer.parseInt(val1) + Integer.parseInt(val2);
			System.out.println(result);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public static String reverse(StringBuffer sb) {
		return sb.reverse().toString();
	}

}


你可能感兴趣的:(java语言,整数翻转后相加,校园招聘笔试题)