package cn.sub2.sub2;import java.util.Scanner;public class sub2 {
public static void main(String[] args) {
final int add1Max = 1000;// 可计算最长位数
final int add2Max = 1000; int i, ka, kb; char[] a2 = new char[add1Max];
char[] b2 = new char[add1Max];
String a1;
String b1; Scanner in = new Scanner(System.in);
a1 = in.nextLine();// 输入高精度数;
b1 = in.nextLine();
ka = a1.length();// 获取长度;
kb = b1.length();
for (i = 0; i < a1.length(); i++)
a2[i] = a1.charAt(i);// 将输入的高精度数,存入字符数组中;
for (i = 0; i < b1.length(); i++)
b2[i] = b1.charAt(i); int[] a = new int[ka];
int[] b = new int[kb];
for (i = 0; i < ka; i++)
a[i] = a2[ka - i - 1] - '0';// 将字符数组中的元素变为整数存入整形数组; for (i = 0; i < kb; i++)
b[i] = b2[kb - i - 1] - '0'; int m = 0;
for (i = a.length - 1; i >= 0; i--) {
// 进行减法运算;
m = a[i] - b[i];
if (m < 0) {
// 如果被减数的这一位小于减数;
a[i] = m + 10;// 因为减得的是负数,所以加上10;
a[i - 1] -= 1;// 再从前一位减一;
} else
a[i] = m; }
for (i = ka - 1; i >= 0; i--)
System.out.printf("%d", a[i]); }
}

解决方案 »

  1.   

    for (i = a.length - 1; i >= 0; i--) 
    a[i - 1] -= 1;// 再从前一位减一;
    数组越界
      

  2.   

    a[i - 1] -= 1;// 再从前一位减一;
    这里越位了,当i为0的时候成了a[-1]1=1;
      

  3.   

    java.lang.ArrayIndexOutOfBoundsException 
    你的数组越界了!
      

  4.   

    给你个例子:
    public class Sub2 {
    private String value;

    public Sub2(String minuend, String subtrahend) {
    this.subtract(minuend, subtrahend);
    }

    public String toString() {
    return this.value;
    }

    private boolean subtractIntStr(String minuend, String subtrahend, boolean isAppendZreo, boolean isBorrowed) {
    int loops = Math.max(minuend.length(), subtrahend.length());
    if(isAppendZreo) {
    for(int i = minuend.length(); i < loops; i++) {
    minuend += "0";
    }
    for(int i = subtrahend.length(); i < loops; i++) {
    subtrahend += "0";
    }
    } else {
    for(int i = minuend.length(); i < loops; i++) {
    minuend = "0" + minuend;
    }
    for(int i = subtrahend.length(); i < loops; i++) {
    subtrahend = "0" + subtrahend;
    }
    }
    int minuendBit;
    int subtrahendBit;
    for(int i = loops - 1; i >= 0; i--) {
    minuendBit = minuend.charAt(i) - 48;
    if(isBorrowed) {
    minuendBit--;
    isBorrowed = false;
    }
    subtrahendBit = subtrahend.charAt(i) - 48;
    if(minuendBit < subtrahendBit) {
    minuendBit += 10;
    isBorrowed = true;
    }
    this.value = Integer.toString(minuendBit - subtrahendBit) + this.value;
    }

    return isBorrowed;
    }

    private void subtract(String minuend, String subtrahend) {
    int index = minuend.indexOf('.');
    String minuendInteger = minuend;
    String minuendFract = "";
    if(index >= 0 && index < minuend.length()) {
    minuendInteger = minuend.substring(0, index);
    minuendFract = minuend.substring(index + 1);
    }
    index = subtrahend.indexOf('.');
    String subtrahendInteger = subtrahend;
    String subtrahendFract = "";
    if(index >= 0 && index < subtrahend.length()) {
    subtrahendInteger = subtrahend.substring(0, index);
    subtrahendFract = subtrahend.substring(index + 1);
    }
    this.value = "";
    boolean isBorrowed = this.subtractIntStr(minuendFract, subtrahendFract, true, false);
    this.value = "." + this.value;
    isBorrowed = this.subtractIntStr(minuendInteger, subtrahendInteger, false, isBorrowed);

    while(this.value.length() > 1) {
    if(this.value.charAt(0) == '0') {
    if(this.value.charAt(1) == '.') {
    break;
    }
    this.value = this.value.substring(1);
    } else {
    break;
    }
    }

    if(isBorrowed) {
    String newMinuend  = "1";
    index = this.value.indexOf('.');
    if(!(index >= 0 && index < this.value.length())) {
    index = this.value.length();
    }
    for(int i = 0; i < index; i++) {
    newMinuend += "0";
    } String newSubtrahend = new String(this.value);
    this.value = "";
    this.subtract(newMinuend, newSubtrahend);
    this.value = "-" + this.value;
    }
    int lastIndex = this.value.length() - 1;
    if(this.value.charAt(lastIndex) == '.') {
    this.value = this.value.substring(0, lastIndex);
    }
    }

    public static void main(String[] args) {
    Sub2 sub2 = new Sub2("10.00", "1234567890123456789012345678901234567890.123456789");
    System.out.println(sub2.toString());
    }
    }
      

  5.   

    那要怎么改正哦,可不可以教一下?7楼的代码没有注释,有些地方看不明。我把里面的main()改成        public static void main(String[] args) {
         Scanner in=new Scanner(System.in);
            //Sub2 sub2 = new Sub2("1000325123512351515", "1223512532523512352151533335234325312513");
            String a=new String();
            String b=new String();
             a=in.nextLine();
             b=in.nextLine();
            Sub2 sub2 = new Sub2(a,b);
            System.out.println(sub2.toString());
        }得到的结果不正确,疑惑ing...
      

  6.   

    public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    String a = in.nextLine();
    String b = in.nextLine();
    Sub2 sub2 = new Sub2(a, b);
    System.out.println(sub2.toString());
    }
    没错阿