我编写了一个Java程序,该程序从左到右评估数学表达式(没有优先权,从左到右)。但是,我没有得到所需的输出。
import java.util.*;
public class Evaluation {
//private static final char[] validOperators = {'/','*','+','-'};
private Evaluation()
{
/* Using a private contructor to prevent instantiation
Using class as a simple static utility class
*/
}
private static int evaluate(String leftSide, char oper, String rightSide)
throws IllegalArgumentException
{
System.out.println("Evaluating: " + leftSide + " (" + oper + ") " + rightSide);
int total = 0;
int leftResult = 0;
int rightResult = 0;
String originalString =leftSide;
int operatorLoc = findOperatorLocation(leftSide);
leftSide = leftSide.substring(0,operatorLoc);
rightSide = originalString.substring(operatorLoc+1,operatorLoc+2);
String remainingString = originalString.substring(operatorLoc+2,originalString.length());
System.out.println("leftSide -->"+leftSide);
System.out.println("rightSide -->"+rightSide);
System.out.println("remainingString --->"+remainingString);
try {
leftResult = Integer.parseInt(leftSide);
} catch(Exception e) {
throw new IllegalArgumentException(
"Invalid value found in portion of equation: "
+ leftSide);
}
try {
rightResult = Integer.parseInt(rightSide);
} catch(Exception e) {
throw new IllegalArgumentException(
"Invalid value found in portion of equation: "
+ rightSide);
}
System.out.println("Getting result of: " + leftResult + " " + oper + " " + rightResult);
switch(oper)
{
case '/':
total = leftResult / rightResult; break;
case '*':
total = leftResult * rightResult; break;
case '+':
total = leftResult + rightResult; break;
case '-':
total = leftResult - rightResult; break;
default:
throw new IllegalArgumentException("Unknown operator.");
}
System.out.println("Returning a result of: " + total);
String totally = String.valueOf(total)+remainingString;
return evaluate(totally,findCharacter(totally),remainingString);
}
private static int findOperatorLocation(String string) {
int index = -1;
index = string.indexOf(string.substring(1,2));
if(index >= 0) {
return index;
}
return index;
}
private static char findCharacter(String string) {
char c='\u0000';
int index = -1;
index = string.indexOf(string.substring(1,2));
if(index >= 0){
c = string.charAt(index);
return c;
}
return c;
}
public static int processEquation(String equation)
throws IllegalArgumentException
{
return evaluate(equation,'+',"0");
}
public static void main(String[] args)
{
//String usage = "Usage: java MathParser equation\nWhere equation is a series"
// + " of integers separated by valid operators (+,-,/,*)";
//if(args.length < 1 || args[0].length() == 0)
// System.out.println(usage);
Scanner input = new Scanner(System.in);
System.out.print("Enter the equation to be evaluated ");
String equation = (String)input.next();
int result = Evaluation.processEquation(equation);
System.out.println("The result of your equation ("
+ equation + ") is: " + result);
//catch(IllegalArgumentException iae)
//{
// System.out.println(iae.getMessage() + "\n" + usage);
//}
}
}
这是我要使用的输入以及期望的输入:
3 + 5 * 2-5
=> 8 * 2-5
=> 16-5
=>预期产量:11
但是我得到以下输出:
输入要评估的方程式3 + 5 * 2-5
评估:3 + 5 * 2-5(+)0
leftSide-> 3
右侧-> 5
剩余的字符串---> * 2-5
得到的结果:3 + 5
返回结果:8
评估:8 * 2-5(*)* 2-5
左侧-> 8
右侧-> 2
剩余的字符串--->-5
得到的结果是:8 * 2
返回结果:16
评价:16-5(6)-5
leftSide-> 1
右侧->-
剩余的字符串---> 5
线程“主”中的异常java.lang.IllegalArgumentException:在等式的一部分中找到了无效的值:-
在Evaluation.evaluate(Evaluation.java:49)
在Evaluation.evaluate(Evaluation.java:70)
在Evaluation.evaluate(Evaluation.java:70)
在Evaluation.processEquation(Evaluation.java:98)
在Evaluation.main(Evaluation.java:112)
对于输入的任何方程式,我无法使我的程序通用。
感谢您提供的任何帮助。
请注意,这不是作业问题。
最佳答案
之所以出现此错误,是因为rightSide
的字符串值为“-”,并且您尝试对其执行Integer.parseInt()
。