-4

此代码应该接收两个整数和一个操作,然后计算数字。这只是整个程序的一部分,其他一切正常,但计算错误。当我运行整个过程时,我得到的唯一正确的输出是减法。它出什么问题了?

public static double calculate(int operand1, int operand2, char operation)
    { 
        if (operation=='^')
        {
            return (Math.pow(operand1,operand2));
         }
        else if(operation=='+')
        {
            return ((operand1)+(operand2));
        }
        else if(operation=='-')
        {
            return (operand1-operand2);
        }
        else if(operation=='*')
        {
            return (operand1*operand2);
        }
        else
        {
            if (operand2==0)
            {
                System.out.println("Cant divide by zero");
            }

            return(operand1/operand2);
        }

    } 

这是程序的完整代码

import java.util.Scanner; 
public class Calculator 
{ 
public static void main(String[] args) 
{ 
    printIntro();
    Scanner kb = new Scanner(System.in); 
    String answer = "yes"; 
    while (answer.equals("yes"))  
{ 
 System.out.println("Enter your problem");
 String fullExpression=kb.nextLine(); 
 fullExpression=fullExpression.replaceAll("\\s","");
 int length=fullExpression.length();
 char op1=fullExpression.charAt(0);
 char op2=fullExpression.charAt(1);
 String operation=fullExpression.substring(2,length);
 printEnglish(op1,op2,operation);
 System.out.println("Type yes to continue");
 Scanner kb1 = new Scanner(System.in);
 answer=kb1.nextLine();

}; 
} 


/*this methods gets the operands and the operation and 
prints the English version: if we call this method 
printEnglish(2,3, plus), then this method will output: 
Two plus three = 5 */ 
public static void printEnglish(char op1, char op2, String operation)  
{ 

 String resultOp1CharToString=charToString(op1);

 String resultOp2CharToString=charToString(op2);  
 char resultOperationConversion=operationConversion(operation);
 double resultCalculate=calculate(op1, op2, resultOperationConversion); 
 System.out.print(resultOp1CharToString+ operationConversion(operation) + resultOp2CharToString+ "=" + resultCalculate); 

    /*1. call the method charToString(op1) to convertlish 
    word for example ‘1’ to one or ‘2’ to two,…. 
    2. call the method operandConversionToNumber(op1) to 
    get its numeric value. For example if op1 is ‘1’ then 
    this method call should return the integer value 1 
    3. call the method operationConversion(operation) to 
    convert the operation to a mathematical operation. For 
    example if you call this method with the string plus 
    then it will return ‘+’ 
    4. finally call the method calculate to get the result 
    of the operation.*/
} 


/*this method prints the numeric version which is 2 *3 
=6*/
//public static boolean printNumeric(char op1, char op2, String operation)  
//{ 
 /*String resultCharToString=charToString(op1);
 String resultCharToString2=charToString(op2);  
 int resultOperandToNumber=operandConversionToNumber(op1);
 int resultOperandToNumber2=operandConversionToNumber(op2);
 char resultOperationConversion=operationConversion(operation);
 double resultCalculate=calculate(op1, op2, operation); */  

//} 


/*this method gets a number as a character and returns 
its numeric value as an integer. You must use case 
statement for this method*/ 
public static int operandConversiontoNumber(char operand) 
{ 
    int numberOperand=0;
    switch(operand)
    {
        case '0':
            numberOperand=0;
            break;
        case '1':
            numberOperand=1;
            break;
        case '2':
            numberOperand=2;
            break;
        case '3':
            numberOperand=3;
            break;
        case '4':
            numberOperand=4;
            break;
        case '5':
            numberOperand=5;
            break;
        case '6':
            numberOperand=6;
            break;
        case '7':
            numberOperand=7;
            break;
        case '8':
            numberOperand=8;
            break;
        case '9':
            numberOperand=9;
            break;
    }
    return numberOperand;

} 


/*this method gets the operation as a string and 
return the equivalent operation in math. For example 
if it receives “plus” the it will return ‘+’ */ 
public static char operationConversion(String s) 
{ 
    char operation=0;
    if(s.equals("plus"))
    {
        operation= '+';
    }

    else if(s.equals("minus"))
    {
        operation= '-';
    }

    else if(s.equals("multiply"))
    {
        operation= '*';
    }

    else if(s.equals("divide"))
    {
        operation= '/';
    }
    else
    {
        operation= '^';
    }

    return operation;


} 



/*this method recives two numbers and the operation
and returns the result*/ 
public static double calculate(int operand1, int operand2, char operation)
{ 
    if (operation=='^')
    {
        return (Math.pow(operand1,operand2));
    }
    else if(operation=='+')
    {
        return ((operand1)+(operand2));
    }
    else if(operation=='-')
    {
        return (operand1-operand2);
    }
    else if(operation=='*')
    {
        return (operand1*operand2);
    }
    else
    {
        if (operand2==0)
        {
            System.out.println("Cant divide by zero");
        }

        return(operand1/operand2);
    }

} 


/*this method converst a number character to its 
English word for example if this method receives ‘1’ 
it will return “one” */ 
public static String charToString(char num) 
{           

    String englishOperand="one";
    switch(num)
    {
        case '0':
            englishOperand= "zero";
            break;
        case '1':
            englishOperand="one";
            break;
        case '2':
            englishOperand="two";
            break;
        case '3':
            englishOperand="three";
            break;
        case '4':
            englishOperand= "four";
            break;
        case '5':
            englishOperand= "five";
            break;
        case '6':
            englishOperand= "six";
            break;
        case '7':
            englishOperand= "seven";
            break;
        case '8':
            englishOperand= "eight";
            break;
        case '9':
            englishOperand= "nine";
            break;
    }
    return englishOperand;
        }


//this method prints the decription of this program. 
public static void printIntro() 
{ 
    System.out.println("This program is a calculator, you need to enter two");
    System.out.println("single digit numbers and an operation(plus, minus,");
    System.out.println("divide, multiply, power) and it outputs its numeric");
    System.out.println("and English version. Your operand and operation can be");
    System.out.println("separated by space(s) or there could be no spaces"); 
    System.out.println("between them. For example you can enter “23plus” or “2 3 plus”"); 

}

}

我输入了 2 3 plus 我期望 5 但我没有收到,但是当我输入 2 3 minus 我收到 -1

4

2 回答 2

1
if (operand2==0)
{
    System.out.println("Cant divide by zero");
    return -1; // some dummy value
}
else
{
    return(operand1/operand2);
}
于 2013-04-02T07:42:21.467 回答
0

除法将不正确,因为它正在执行整数除法并且您想要双倍结果。由于您不返回,它还会引发除以零错误。

if (operand2 == 0)
{
    System.out.println("Cant divide by zero");
    return 0; // I guess
}
return (double)operand1 / (double)operand2;

其余的看起来应该可以正常工作。

于 2013-04-02T07:43:22.657 回答