如何调试我的非常规 Java 计算器?
How to debug my unconventional Java calculator?
我刚刚完成了一个计算器的编程,该计算器执行四种基本算术运算,外加六种三角运算,界面非常简约,用户只需输入表达式并显示结果,无需单独输入操作数和运算符。
我认为它可以为用户节省时间,并且看起来会更完整,因为没有更好的词了,如果它能起作用的话。
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 中的一个致命错误引起的。我现在已经修复了代码,修复后的代码如下。
您在 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。
解决方法如下:
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.*;
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 中的一个致命错误引起的。我现在已经修复了代码,修复后的代码如下。
您在 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。
解决方法如下:
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);
}
}
}