计算代码有问题吗?

时间:2013-04-02 07:39:06

标签: java if-statement

这段代码应该接收两个整数和一个操作然后计算数字。这只是整个程序的一部分,其他一切工作但计算错误。我运行整个事情时唯一正确的输出是减法。这有什么不对吗?

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

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

分区是不正确的,因为它正在进行整数除法,你想要一个双重结果。它也会抛出零除错误,因为你没有返回。

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

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