【发布时间】: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