我目前正在研究旨在将字符串解析为链表的代码,并使用该列表作为计算操作的方法。 如所见,我当前将操作字符串设置为"1 + 2",运行时,它返回整数 0。
非常感谢所有帮助。
import java.awt.Dimension;
import java.util.LinkedList;
import java.util.List;
public class LinkedListCalculator {
static List<String> CalcInput = new LinkedList<String>();
static String operation = "1+2";
static int equationLength = operation.length();
static int result = 0;
static int num = 0;
static int mul = 1;
static int finalNum = 0;
static char lastOperator = 0;
public static void addToList() {
String finalNumString;
if (lastOperator == '+') {
finalNumString = Integer.toString(finalNum);
CalcInput.add(finalNumString);
CalcInput.add("+");
} else if (lastOperator == '-') {
finalNumString = Integer.toString(finalNum);
CalcInput.add(finalNumString);
CalcInput.add("-");
} else if (lastOperator == '*') {
finalNumString = Integer.toString(finalNum);
CalcInput.add(finalNumString);
CalcInput.add("*");
} else if (lastOperator == '/') {
finalNumString = Integer.toString(finalNum);
CalcInput.add(finalNumString);
CalcInput.add("/");
}
}
public static void computeList() {
int size = CalcInput.size();
String lastIndex = null;
for (int i = 0; i > size; i++) {
String currentIndex = CalcInput.get(i);
if (i > 0) {
lastIndex = CalcInput.get(i - 1);
}
if (currentIndex == "+") {
int lastIndexInt = Integer.valueOf(lastIndex);
result += lastIndexInt;
}
if (currentIndex == "-") {
int lastIndexInt = Integer.valueOf(lastIndex);
result -= lastIndexInt;
}
if (currentIndex == "*") {
int lastIndexInt = Integer.valueOf(lastIndex);
result *= lastIndexInt;
}
if (currentIndex == "/") {
int lastIndexInt = Integer.valueOf(lastIndex);
result /= lastIndexInt;
}
}
}
public static void main(String [] args) {
for (int currEqnCharNdx = 0; currEqnCharNdx < equationLength ; currEqnCharNdx++) {
char j = operation.charAt(currEqnCharNdx);
if (j >= '0' && j <= '9') {
num = j - '0';
finalNum *= mul;
finalNum += num;
mul = 10;
} else if (operation.charAt(currEqnCharNdx) == '+') {
addToList();
lastOperator = '+';
mul = 1;
finalNum = 0;
} else if (operation.charAt(currEqnCharNdx) == '-') {
addToList();
lastOperator = '-';
mul = 1;
finalNum = 0;
} else if (operation.charAt(currEqnCharNdx) == '*') {
addToList();
lastOperator = '*';
mul = 1;
finalNum = 0;
} else if (operation.charAt(currEqnCharNdx) == '/') {
addToList();
lastOperator = '/';
mul = 1;
finalNum = 0;
}
}
int i = CalcInput.size();
computeList();
System.out.println("result: " + result);
}
}
你的代码有很多问题。以下是一些指向错误的指针:
String lastIndex = null;
for (int i = 0; i > size; i++) {
这将永远不会执行(更改为 i <大小)>
我也怀疑addToList()应该在设置lastOperator等之后发生。
else if (operation.charAt(currEqnCharNdx) == '+') {
addToList();
lastOperator = '+';
mul = 1;
finalNum = 0;
}
另外,在这里(在addToList()中):
if (lastOperator == '+') {
finalNumString = Integer.toString(finalNum);
CalcInput.add(finalNumString);
CalcInput.add("+");
}
你加上最后一个数字,加上 + 号,但没有什么会导致 2 被添加,所以当它在最后求和时(如果你修复我提到的其他事情),它将对 1+ 求和,结果为 1(而不是 1+2 使结果为 3)。
这是它输出 1(见上面最后一点)供 OP 重现的代码:
import java.awt.Dimension;
import java.util.LinkedList;
import java.util.List;
import java.util.StringTokenizer;
public class LinkedListCalculator {
static List<String> CalcInput = new LinkedList<String>();
static String operation = "1+2";
static int equationLength = operation.length();
static int result = 0;
static int num = 0;
static int mul = 1;
static int finalNum = 0;
static char lastOperator = 0;
public static void addToList() {
String finalNumString;
if (lastOperator == '+') {
finalNumString = Integer.toString(finalNum);
CalcInput.add(finalNumString);
CalcInput.add("+");
} else if (lastOperator == '-') {
finalNumString = Integer.toString(finalNum);
CalcInput.add(finalNumString);
CalcInput.add("-");
} else if (lastOperator == '*') {
finalNumString = Integer.toString(finalNum);
CalcInput.add(finalNumString);
CalcInput.add("*");
} else if (lastOperator == '/') {
finalNumString = Integer.toString(finalNum);
CalcInput.add(finalNumString);
CalcInput.add("/");
}
}
public static void computeList() {
int size = CalcInput.size();
String lastIndex = null;
for (int i = 0; i < size; i++) {
String currentIndex = CalcInput.get(i);
if (i > 0) {
lastIndex = CalcInput.get(i - 1);
}
if (currentIndex == "+") {
int lastIndexInt = Integer.valueOf(lastIndex);
result += lastIndexInt;
}
if (currentIndex == "-") {
int lastIndexInt = Integer.valueOf(lastIndex);
result -= lastIndexInt;
}
if (currentIndex == "*") {
int lastIndexInt = Integer.valueOf(lastIndex);
result *= lastIndexInt;
}
if (currentIndex == "/") {
int lastIndexInt = Integer.valueOf(lastIndex);
result /= lastIndexInt;
}
}
}
public static void main(String [] args) {
for (int currEqnCharNdx = 0; currEqnCharNdx < equationLength ; currEqnCharNdx++) {
char j = operation.charAt(currEqnCharNdx);
if (j >= '0' && j <= '9') {
num = j - '0';
finalNum *= mul;
finalNum += num;
mul = 10;
} else if (operation.charAt(currEqnCharNdx) == '+') {
lastOperator = '+';
addToList();
mul = 1;
finalNum = 0;
} else if (operation.charAt(currEqnCharNdx) == '-') {
addToList();
lastOperator = '-';
mul = 1;
finalNum = 0;
} else if (operation.charAt(currEqnCharNdx) == '*') {
addToList();
lastOperator = '*';
mul = 1;
finalNum = 0;
} else if (operation.charAt(currEqnCharNdx) == '/') {
addToList();
lastOperator = '/';
mul = 1;
finalNum = 0;
}
}
int i = CalcInput.size();
computeList();
System.out.println("result: " + result);
}
}