2012-03-24 174 views
0

我需要製作一個程序,不使用biginteger類就可以添加長整數。方法中的錯誤返回類型

我現在正在研究add方法,我認爲我的方法正確,但我堅持爲我的方法返回正確的數據類型,我不確定如何更正它。

這裏是我的2班至今:

主類:

import java.util.Scanner; 

public class testLargeInteger 
{ 
    public static void main(String[] args) 
    { 
     Scanner input = new Scanner(System.in); 
     String string1; 
     String string2; 
     int exp =0; 


     System.out.print("Enter the first integer: "); 
     //Store up the input string 「string1」 entered by the user from the keyboard. 
     string1 = input.next(); 

     LargeInteger firstInt = new LargeInteger(string1); 

     System.out.print("Enter the second integer: "); 
     string2 = input.next(); 
     //Store up the input string 「string2」 entered by the user from the keyboard. 
     LargeInteger secondInt = new LargeInteger(string2); 

     System.out.print("Enter the exponential integer: "); 
     //Store up the input integer 「exp」 entered by the user from the keyboard. 
     exp = input.nextInt(); 


     LargeInteger sum = firstInt.add(secondInt); 

     System.out.printf ("First integer: %s \n", firstInt.display()); 
     System.out.println("Second integer: " + secondInt.display()); 
     System.out.println(" Exponent: " + exp); 

     System.out.printf (" Sum = %s \n", sum.display()); 

    } 
} 

LargeInteger.class:

public class LargeInteger 
{ 
    private int[] intArray; 

    //convert the strings to array 
    public LargeInteger(String s) 
    { 
     intArray = new int[s.length()]; 
     for (int i = 0; i < s.length(); i++) 
      intArray[i] = Character.digit(s.charAt(i), 10); // in base 10 
    } 

    //display the strings 
    public String display() 
    {   
     String result=""; 

     for (int i = 0; i < intArray.length; i++)  
      result += intArray[i]; 
     return result.toString(); 
    } 

    //get first array 
    public int[] getIntArray() 
    { 
     return intArray; 
    } 

    public LargeInteger add(LargeInteger secondInt) 
    { 
     int[] otherValues = secondInt.getIntArray(); 

     int maxIterations = Math.min(intArray.length, otherValues.length); 
     int currentResult; //to store result 
     int[] resultArray = new int[Math.max(intArray.length, otherValues.length) + 1]; 

     int needToAdd = 0; //to store result should be added next step 

     for(int i = 0; i < maxIterations; i++) 
     { 
      currentResult = intArray[i] + otherValues[i]; 
      resultArray[i] = currentResult % 10 + needToAdd; //if more than 9 its correct answer 
      needToAdd = currentResult/10; //this is what you need to add on next step 
     } 
     resultArray[Math.max(intArray.length, otherValues.length) + 1] = needToAdd; 

     return resultArray; 
    } 
} 

回答

1

你需要一個第二LargeInteger構造:

public LargeInteger(int[] array) { 
    intArray = array 
} 

那麼你的方法可以返回:

return new LargeInteger(resultArray); 
+0

謝謝你的作品。現在我需要看到什麼毛病我陣列..得到一個「ArrayIndexOutOfBoundsException」錯誤。 – Sackling 2012-03-24 22:44:05

1

那麼第一件事情,你的方法LargeIntegeradd應該返回一個LargeInteger,而是返回一個數組(int[])。您可以通過添加LargeInteger的構造函數來解決此問題,該構造函數需要參數int[](即LargeInteger(int[] digitArray))。然後,在您的add方法中,您可以簡單地執行:return new LargeInteger(resultArray);