Java 计算代码有问题吗?

Java 计算代码有问题吗?,java,if-statement,Java,If Statement,这段代码应该接收两个整数和一个运算,然后计算数字。这只是整个程序的一部分,其他的都可以,但是计算是错误的。当我运行整个程序时,唯一正确的输出是减法。怎么了 public static double calculate(int operand1, int operand2, char operation) { if (operation=='^') { return (Math.pow(operand1,operand2));

这段代码应该接收两个整数和一个运算,然后计算数字。这只是整个程序的一部分,其他的都可以,但是计算是错误的。当我运行整个程序时,唯一正确的输出是减法。怎么了

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加号,我希望是5,但我没有收到,但是当我输入2 3减号时,我收到-1除法是不正确的,因为它是整数除法,你想要一个双精度的结果。它还会抛出一个被零除的错误,因为您不返回

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

其他所有代码看起来都应该可以正常工作。

您需要提供更多代码。您在哪里调用
计算
?结果以何种方式出错?你期望什么,实际结果是什么,这些有什么不同?你所说的错误是什么意思?示例输入/预期输出?您有一个逻辑错误,但它与您描述的症状不匹配(在您描述症状的程度上):如果您尝试
/
,您将得到一个错误,因为尽管您检查
操作数2==0
,但您仍然继续并尝试使用它进行除法。我将使用
开关来代替所有这些ifs。另外,你能举一个例子,当这个代码不能产生预期的输出吗?