2012-03-24 41 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コンストラクタを必要とします"ArrayIndexOutOfBoundsException"エラーが発生しました。
+0

ありがとう作品:

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

は、その後、あなたのメソッドが返すことができます。 – Sackling

1

まあ最初、あなたの方法LargeIntegeraddを返すことになっていますa LargeIntegerではなく、配列(int[])を返します。 int[]パラメータ(つまりLargeInteger(int[] digitArray))を受け取るLargeIntegerのコンストラクタを追加することでこれを修正できます。次に、addメソッドでは、単にreturn new LargeInteger(resultArray);とすることができます。今、私は私の配列で間違っ何を参照する必要があります..取得を

return new LargeInteger(resultArray); 
関連する問題