Java 方法中的返回类型错误

Java 方法中的返回类型错误,java,Java,我需要制作一个程序,在不使用biginteger类的情况下添加长整数 我现在正在研究add方法,我想我的方法是正确的,但是我一直在为我的方法返回正确的数据类型,我不知道如何更正它 以下是迄今为止我的两门课: 主要类别: import java.util.Scanner; public class testLargeInteger { public static void main(String[] args) { Scanner input = new Scan

我需要制作一个程序,在不使用biginteger类的情况下添加长整数

我现在正在研究
add
方法,我想我的方法是正确的,但是我一直在为我的方法返回正确的数据类型,我不知道如何更正它

以下是迄今为止我的两门课:

主要类别:

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;
    }
}
公共类大整数
{
私人内部[]阵列;
//将字符串转换为数组
公共大整数(字符串s)
{   
intArray=newint[s.length()];
对于(int i=0;i
首先,在
LargeInteger
中,您的方法
add
应该返回一个
LargeInteger
,但会返回一个数组(
int[]
)。您可以通过为
LargeInteger
添加一个构造函数来修复此问题,该构造函数接受
int[]
参数(即
LargeInteger(int[]digitArray)
)。然后,在
add
方法中,您可以简单地执行以下操作:
返回新的大整数(resultArray)

您需要第二个
LargeInteger
构造函数:

public LargeInteger( int[] array ) { 
     intArray = array
}
然后,您的方法可以返回:

return new LargeInteger( resultArray );

谢谢你,这很有效。。现在我需要看看我的阵列出了什么问题。。获取“ArrayIndexOutOfBoundsException”错误。