import java.util.*; /*请设计一个算法能够完成两个用字符串存储的整数进行相加操作,对非法输入则返回“error”; * 用例:123 234 * 输出:357 * 用例123 asd * 输出:error * 输入:1234 12 * 输出:2434*/ public class stringadd { public static void main(String[] args){ Scanner cin = new Scanner(System.in); String str = cin.nextLine(); String[] st = str.split(" "); int[] result = new int[max(st[0],st[1])]; if(!isNumber(st[0])||!isNumber(st[1])){ System.out.print("error"); }else{ result = add(st[0],st[1]); for(int i=0;i<result.length;i++){ System.out.print(result[i]); } } } public static int max(String a,String b){ if(a.length()>b.length()){ return a.length(); }else{ return b.length(); } } public static int min(String a,String b){ if(a.length()>b.length()){ return b.length(); }else{ return a.length(); } } public static int[] add(String a,String b){ int[] x = new int[max(a,b)]; int len = min(a,b); String l = a; if(a.length()<b.length()){ l=b; } for(int i=0;i<len;i++){ x[i]=(a.charAt(i)-48)+(b.charAt(i)-48);//0的ASCII值为48 } for(int j=len;j<l.length();j++){ x[j]=l.charAt(j)-48; } return x; } public static boolean isNumber(String str){ for(int i=0;i<str.length();i++){ if(!Character.isDigit(str.charAt(i))){ return false; } } return true; }; } /*字符1+字符2是ASCII值相加,是49+50,不是1+2*/