如何调试我的非传统Java计算器?

如何调试我的非传统Java计算器?,java,debugging,calculator,Java,Debugging,Calculator,我刚刚完成了一个计算器的编程,它执行四个基本算术运算,加上六个三角运算,界面非常简洁,用户只需输入表达式并显示结果,无需单独输入操作数和运算符 我认为它为用户节省了时间,如果它能工作的话,因为没有更好的词,它看起来会更完整 import java.util.*; public class Calculator { public static String getOperator(String expression) { int counter=0;

我刚刚完成了一个计算器的编程,它执行四个基本算术运算,加上六个三角运算,界面非常简洁,用户只需输入表达式并显示结果,无需单独输入操作数和运算符

我认为它为用户节省了时间,如果它能工作的话,因为没有更好的词,它看起来会更完整

import java.util.*;

public class Calculator
{
public static String getOperator(String expression)          
{
    int counter=0;                                          

    for (int i=0; i<3; i++)                                 
    {
        if (Character.isLetter(expression.charAt(i)))      
            counter++;                                        
    }                                                      

    if (counter==3)                                       
        return expression.substring(0, 3);
    else
    {
        for (int j=0; j<expression.length(); j++)
        {
            if (Character.isDigit(expression.charAt(j))==false)
            {
              if (expression.charAt(j)!='.')
                return Character.toString(expression.charAt(j));
            }
        }
    }

    return "false";
}

public static double getFirstOperand(String operator, String expression)
{
    return Double.parseDouble(expression.substring(expression.indexOf(operator)+1));
}

public static String getOperatorType(String expression)
{
    int counter=0;

    for (int i=0; i<3; i++)
    {
        if (Character.isLetter(expression.charAt(i)))
            counter++;
    }

    if (counter==3)
        return "Trigonometrical";
    else
        return "Arithemtic";
}

public static double getResult(String operator, double operand)
{
    if (operator.equals("sin"))
        return Math.sin(operand);
    if (operator.equals("cos"))
        return Math.cos(operand);
    if (operator.equals("tan"))
        return Math.tan(operand);
    if (operator.equals("cot"))
        return 1/Math.tan(operand);
    if (operator.equals("cosec"))
        return 1/Math.sin(operand);
    else
        return 1/Math.cos(operand);      
}

public static double getSecondOperand(String expression)
{
    return Double.parseDouble(expression.substring(0, expression.indexOf(expression)));
}

public static double getResult(String operator, double operand1, double operand2)
{
    if (operator.equals("*"))
        return operand1*operand2;
    if (operator.equals("+"))
        return operand1+operand2;
    if (operator.equals("/"))
        return operand2/operand1;
    else
        return operand2-operand1;
}

public static void main(String[] args)
{
    Scanner sc = new Scanner(System.in);
    String command="", operator="", operatorType="";
    double operand1=0.0, operand2=0.0, result=0.0;

    while (command.equals("EXIT")=false)
    {
        System.out.println("Enter command: ");
        command = sc.next();

        operator = getOperator(command);
        operand1 = getFirstOperand(operator, command);
        operatorType = getOperatorType(command);

        if (operatorType.equals("Trigonometrical"))
            result=getResult(operator, operand1);
        if (operatorType.equals("Arithmetic"))
        {
            operand2 = getSecondOperand(command);
            result=getResult(operator, operand1, operand2); 
        }

        System.out.println("Result="+result);
    }
}
}
我不明白问题出在哪里。我已经在代码中搜索了几十次,但我就是看不到

更新:谢谢你们的帮助,伙计们。计算器终于正常工作了。事实上,几乎所有的问题都是由GetSecondOperator中的一个致命错误引起的。我现在已经修复了代码,下面给出了修复后的代码。

您在
getOperatorType()中的
算术“
中有一个打字错误:

公共静态字符串getOperatorType(字符串表达式)
{
int计数器=0;
对于(int i=0;i,以下是解决方案:

import java.util.*;

public class Calculator
{
public static String getOperator(String expression)
{
    int counter=0;

    for (int i=0; i<3; i++)
    {
        if (Character.isLetter(expression.charAt(i)))
            counter++;
    }

    if (counter==3)
        return expression.substring(0, 3);
    else
    {
        for (int j=0; j<expression.length(); j++)
        {
            if (Character.isDigit(expression.charAt(j))==false)
                return Character.toString(expression.charAt(j));
        }
    }

    return "false";
}

public static double getFirstOperand(String operator, String expression)
{
    return Double.parseDouble(expression.substring(expression.lastIndexOf(operator)+1));
}

public static String getOperatorType(String expression)
{
    int counter=0;

    for (int i=0; i<3; i++)
    {
        if (Character.isLetter(expression.charAt(i)))
            counter++;
    }

    if (counter==3)
        return "Trigonometrical";
    else
        return "Arithmetic";
}

public static double getResult(String operator, double operand)
{
    if (operator.equals("sin"))
        return Math.sin(operand);
    if (operator.equals("cos"))
        return Math.cos(operand);
    if (operator.equals("tan"))
        return Math.tan(operand);
    if (operator.equals("cot"))
        return 1/Math.tan(operand);
    if (operator.equals("cosec"))
        return 1/Math.sin(operand);
    else
        return 1/Math.cos(operand);      
}

public static double getSecondOperand(String expression, String operator)
{
    return Double.parseDouble(expression.substring(0, expression.indexOf(operator)));
}

public static double getResult(String operator, double operand1, double operand2)
{
    if (operator.equals("*"))
        return operand1*operand2;
    if (operator.equals("+"))
        return operand1+operand2;
    if (operator.equals("/"))
        return operand2/operand1;
    else
        return operand2-operand1;
}

public static void main(String[] args)
{
    Scanner sc = new Scanner(System.in);
    String command="", operator="", operatorType="";
    double operand1=0.0, operand2=0.0, result=0.0;
    char exitNow='0';

    while (exitNow=='0'||exitNow=='N')
    {
        System.out.println("Enter command: ");
        command = sc.next();

        operator = getOperator(command);
        operand1 = getFirstOperand(operator, command);
        operatorType = getOperatorType(command);

        if (operatorType.equals("Trigonometrical"))
            result=getResult(operator, operand1);
        if (operatorType.equals("Arithmetic"))
        {
            operand2 = getSecondOperand(command, operator);
            result=getResult(operator, operand1, operand2); 
        }

        System.out.println("Result="+result+"\nExit now(1/0)(Y/N)");
        exitNow=sc.next().charAt(0);            
    }
}
}
import java.util.*;
公共类计算器
{
公共静态字符串getOperator(字符串表达式)
{
int计数器=0;

对于(int i=0),Iu应该进行一些调试(而不是只看代码)。还考虑<代码>=
和<代码>均衡器()/code >之间的差异;-作为开始,使用<代码>均衡器()/代码>比较字符串,而不是<代码>=
。在这个例子中,您应该使用枚举而不是字符串。我在所有地方都比较了字符串。哦,等等。我想我看到了我错过了equals()的地方。我会在一分钟内返回修复代码的结果。好的,所以我修复了拼写错误和equals()问题。但是现在,我得到了错误:线程“main”中的异常java.lang.NumberFormatException:sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1842)的空字符串位于sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)的java.lang.Double.parseDouble(Double.java:538)的Calculator.main(Calculator.java:103)的GetSecondOperator(Calculator.java:69)的空字符串代码中还有很多错误。我建议您使用调试器来查找。线程“main”java.lang.NumberFormatException中的异常:sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1842)在sun.misc.FloatingDecimal.parseDouble中的空字符串(FloatingDecimal.java:110)在java.lang.Double.parseDouble中在Calculator.main(Calculator.java:103)处的getSecondOperator(Calculator.java:69)处(Double.java:538)确切地说,使用调试器。我们不是来完全调试您的程序的。我已经使用NetBeans对其进行了多次调试。它找不到问题。问题是您试图在第69行的
getSecondOperator()
处将空字符串解析为双精度字符串。
public static String getOperatorType(String expression)
{
    int counter=0;

    for (int i=0; i<3; i++)
    {
        if (Character.isLetter(expression.charAt(i)))
            counter++;
    }

    if (counter==3)
        return "Trigonometrical";
    else
        return "Arithemtic";
}
import java.util.*;

public class Calculator
{
public static String getOperator(String expression)
{
    int counter=0;

    for (int i=0; i<3; i++)
    {
        if (Character.isLetter(expression.charAt(i)))
            counter++;
    }

    if (counter==3)
        return expression.substring(0, 3);
    else
    {
        for (int j=0; j<expression.length(); j++)
        {
            if (Character.isDigit(expression.charAt(j))==false)
                return Character.toString(expression.charAt(j));
        }
    }

    return "false";
}

public static double getFirstOperand(String operator, String expression)
{
    return Double.parseDouble(expression.substring(expression.lastIndexOf(operator)+1));
}

public static String getOperatorType(String expression)
{
    int counter=0;

    for (int i=0; i<3; i++)
    {
        if (Character.isLetter(expression.charAt(i)))
            counter++;
    }

    if (counter==3)
        return "Trigonometrical";
    else
        return "Arithmetic";
}

public static double getResult(String operator, double operand)
{
    if (operator.equals("sin"))
        return Math.sin(operand);
    if (operator.equals("cos"))
        return Math.cos(operand);
    if (operator.equals("tan"))
        return Math.tan(operand);
    if (operator.equals("cot"))
        return 1/Math.tan(operand);
    if (operator.equals("cosec"))
        return 1/Math.sin(operand);
    else
        return 1/Math.cos(operand);      
}

public static double getSecondOperand(String expression, String operator)
{
    return Double.parseDouble(expression.substring(0, expression.indexOf(operator)));
}

public static double getResult(String operator, double operand1, double operand2)
{
    if (operator.equals("*"))
        return operand1*operand2;
    if (operator.equals("+"))
        return operand1+operand2;
    if (operator.equals("/"))
        return operand2/operand1;
    else
        return operand2-operand1;
}

public static void main(String[] args)
{
    Scanner sc = new Scanner(System.in);
    String command="", operator="", operatorType="";
    double operand1=0.0, operand2=0.0, result=0.0;
    char exitNow='0';

    while (exitNow=='0'||exitNow=='N')
    {
        System.out.println("Enter command: ");
        command = sc.next();

        operator = getOperator(command);
        operand1 = getFirstOperand(operator, command);
        operatorType = getOperatorType(command);

        if (operatorType.equals("Trigonometrical"))
            result=getResult(operator, operand1);
        if (operatorType.equals("Arithmetic"))
        {
            operand2 = getSecondOperand(command, operator);
            result=getResult(operator, operand1, operand2); 
        }

        System.out.println("Result="+result+"\nExit now(1/0)(Y/N)");
        exitNow=sc.next().charAt(0);            
    }
}
}