与无限整数java相乘时整数0出现问题

与无限整数java相乘时整数0出现问题,java,integer,multiplication,infinite,Java,Integer,Multiplication,Infinite,我的程序有一个非常奇怪的问题,每当我把一个负数乘以0,答案就会显示为“-0”。无论0是操作中的第一个数字还是第二个数字,都会发生这种情况。例如,如果我将-500和0相乘,-0将是显示的答案。我不知道为什么会这样 public InfiniteIntegerInterface multiply(final InfiniteIntegerInterface anInfiniteInteger) { // TO DO int place1 = 0;

我的程序有一个非常奇怪的问题,每当我把一个负数乘以0,答案就会显示为“-0”。无论0是操作中的第一个数字还是第二个数字,都会发生这种情况。例如,如果我将-500和0相乘,-0将是显示的答案。我不知道为什么会这样

public InfiniteIntegerInterface multiply(final InfiniteIntegerInterface anInfiniteInteger)
    {
            // TO DO
        int place1 = 0;
        int place2 = 0;
        LInfiniteInteger tempFirstOp = new LInfiniteInteger(this.toString());
        LInfiniteInteger tempSecondOp = new LInfiniteInteger(anInfiniteInteger.toString());
        LInfiniteInteger firstOperand = new LInfiniteInteger("");
        LInfiniteInteger secondOperand = new LInfiniteInteger("");
        LInfiniteInteger result = new LInfiniteInteger("");
        LInfiniteInteger tempResult = new LInfiniteInteger("");
        String tempResultString = "";
        Integer tempResultInt = new Integer(0);
        if((tempFirstOp.compareTo(tempSecondOp) == 1) || (tempFirstOp.compareTo(tempSecondOp) == 0))
        {
            firstOperand = tempFirstOp;
            secondOperand = tempSecondOp;
        }
        else
        {
            firstOperand = tempSecondOp;
            secondOperand = tempFirstOp;
        }

        Node currentTwo = secondOperand.lastNode;
    if ((firstOperand.isNegative == true) && (secondOperand.isNegative == false)) {
        while(currentTwo != null)
        {
            place1 = 0;
            Node currentOne = firstOperand.lastNode;
            while(currentOne != null)
            {
                tempResultInt = currentOne.data * currentTwo.data;
                tempResultString = tempResultInt.toString();
                for(int i = 0; i < (place1 + place2); i++)
                {
                    tempResultString = tempResultString + "0";
                }
                tempResult = new LInfiniteInteger(tempResultString);
                result = new LInfiniteInteger(result.plus(tempResult).toString());
                place1++;
                currentOne = currentOne.previous;
            }
            place2++;
            currentTwo = currentTwo.previous;
        }
     result.isNegative = true;
        return result; 
    }  
    if ((firstOperand.isNegative == false) && (secondOperand.isNegative == true)) {
        while(currentTwo != null)
        {
            place1 = 0;
            Node currentOne = firstOperand.lastNode;
            while(currentOne != null)
            {
                tempResultInt = currentOne.data * currentTwo.data;
                tempResultString = tempResultInt.toString();
                for(int i = 0; i < (place1 + place2); i++)
                {
                    tempResultString = tempResultString + "0";
                }
                tempResult = new LInfiniteInteger(tempResultString);
                result = new LInfiniteInteger(result.plus(tempResult).toString());
                place1++;
                currentOne = currentOne.previous;
            }
            place2++;
            currentTwo = currentTwo.previous;
        }
     result.isNegative = true;
        return result; 
    }   

    else {
        while(currentTwo != null)
        {
            place1 = 0;
            Node currentOne = firstOperand.lastNode;
            while(currentOne != null)
            {
                tempResultInt = currentOne.data * currentTwo.data;
                tempResultString = tempResultInt.toString();
                for(int i = 0; i < (place1 + place2); i++)
                {
                    tempResultString = tempResultString + "0";
                }
                tempResult = new LInfiniteInteger(tempResultString);
                result = new LInfiniteInteger(result.plus(tempResult).toString());
                place1++;
                currentOne = currentOne.previous;
            }
            place2++;
            currentTwo = currentTwo.previous;
        }

        return result; 
    }   


  }
public InfiniteIntegerInterface乘法(最终的InfiniteIntegerInterface AnfiniteInteger)
{
//做
int place1=0;
int place2=0;
LInfiniteInteger tempFirstOp=新的LInfiniteInteger(this.toString());
LInfiniteInteger tempSecondOp=新的LInfiniteInteger(一个finiteinteger.toString());
LInfiniteInteger第一个操作数=新的LInfiniteInteger(“”);
LInfiniteInteger第二个操作数=新的LInfiniteInteger(“”);
LInfiniteInteger结果=新的LInfiniteInteger(“”);
LInfiniteInteger tempResult=新的LInfiniteInteger(“”);
字符串tempResultString=“”;
整数tempResultInt=新整数(0);
if((tempFirstOp.compareTo(tempSecondOp)==1)| |(tempFirstOp.compareTo(tempSecondOp)==0))
{
第一个操作数=临时第一个操作数;
第二个操作数=tempSecondOp;
}
其他的
{
第一个操作数=第二个临时操作数;
第二个操作数=临时第一个操作数;
}
节点currentTwo=secondOperand.lastNode;
if((firstOperand.isNegative==true)和&(secondOperand.isNegative==false)){
while(currentTwo!=null)
{
位置1=0;
节点currentOne=firstOperand.lastNode;
while(currentOne!=null)
{
tempResultInt=currentOne.data*currentTwo.data;
tempResultString=tempResultInt.toString();
对于(int i=0;i<(place1+place2);i++)
{
tempResultString=tempResultString+“0”;
}
tempResult=新的LInfiniteInteger(tempResultString);
结果=新的LInfiniteInteger(result.plus(tempResult.toString());
place1++;
currentOne=currentOne.previous;
}
place2++;
currentwo=currentwo.previous;
}
result.isNegative=true;
返回结果;
}  
if((firstOperand.isNegative==false)和&(secondOperand.isNegative==true)){
while(currentTwo!=null)
{
位置1=0;
节点currentOne=firstOperand.lastNode;
while(currentOne!=null)
{
tempResultInt=currentOne.data*currentTwo.data;
tempResultString=tempResultInt.toString();
对于(int i=0;i<(place1+place2);i++)
{
tempResultString=tempResultString+“0”;
}
tempResult=新的LInfiniteInteger(tempResultString);
结果=新的LInfiniteInteger(result.plus(tempResult.toString());
place1++;
currentOne=currentOne.previous;
}
place2++;
currentwo=currentwo.previous;
}
result.isNegative=true;
返回结果;
}   
否则{
while(currentTwo!=null)
{
位置1=0;
节点currentOne=firstOperand.lastNode;
while(currentOne!=null)
{
tempResultInt=currentOne.data*currentTwo.data;
tempResultString=tempResultInt.toString();
对于(int i=0;i<(place1+place2);i++)
{
tempResultString=tempResultString+“0”;
}
tempResult=新的LInfiniteInteger(tempResultString);
结果=新的LInfiniteInteger(result.plus(tempResult.toString());
place1++;
currentOne=currentOne.previous;
}
place2++;
currentwo=currentwo.previous;
}
返回结果;
}   
}

您创建自己的integral类而不是使用Java内置的BigInteger的具体原因是什么?您面临的基本问题是,根据定义0不是正的或负的,因此您的代码应该包含一个isZero()并为这些情况添加
if
语句。。。非常无效…我们必须使用这个类来完成这个赋值,但是我喜欢代码的isZero()思想。谢谢您也可以假定0为正(即使它在数学上不正确),并在运算结束时在右侧进行检查,应用一个操作,即当isNegative()表示零->确保将
-0
替换为0。