【问题标题】:How to debug my unconventional Java calculator?如何调试我的非传统 Java 计算器?
【发布时间】:2023-03-23 14:50:01
【问题描述】:

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

我认为它为用户节省了时间,并且看起来更完整,因为没有更好的词,如果它只管用的话。

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);
    }
}
}

不知何故,无论我输入什么,结果总是 0.0。

Enter command:
45*2
Result=0.0
Enter command:
2+2
Result=0.0

我不明白问题出在哪里。我已经搜索了数十次代码,但我没有看到它。

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

【问题讨论】:

  • 你应该做一些调试(而不是只看你的代码)。
  • 还要考虑==equals()之间的区别;-)
  • 首先,使用equals() 来比较字符串,而不是==。在这种情况下,您应该使用枚举而不是字符串。
  • MattR 我在比较字符串的所有地方都使用了 equals()。等一下。我想我知道我错过了 equals() 的地方。我会在一分钟后返回修复代码的结果。
  • 好的,所以我修正了错字和 equals() 问题。但是现在,我收到错误消息:线程“main”中的异常 java.lang.NumberFormatException: empty String at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1842) at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal. java:110) 在 java.lang.Double.parseDouble(Double.java:538) 在 Calculator.getSecondOperand(Calculator.java:69) 在 Calculator.main(Calculator.java:103)

标签: java debugging calculator


【解决方案1】:

getOperatorType() 中的"Arithmetic" 中有错字:

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";
}

这就是为什么在这种情况下应避免使用字符串,而应使用Enums

此外,您正在使用== 比较字符串,这是行不通的。请改用equals()。或者,改用Enums

【讨论】:

  • 您的代码中还有很多错误。我建议你使用调试器来找出答案。
  • 线程“主”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.getSecondOperand(Calculator.java:69) 在 Calculator.main(Calculator.java:103)
  • 没错,使用调试器。我们不是来完全调试您的程序的。
  • 我已经不止一次地使用 NetBeans 对其进行了调试。它找不到问题。
  • 问题是你试图在第 69 行将一个空字符串解析为 getSecondOperand() 的 Double。
【解决方案2】:

解决办法如下:

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);            
    }
}
}

【讨论】:

    猜你喜欢
    • 2016-07-04
    • 2020-05-10
    • 2010-12-28
    • 1970-01-01
    • 1970-01-01
    • 2015-08-07
    • 2012-04-11
    • 2014-05-04
    • 1970-01-01
    相关资源
    最近更新 更多