将字符串中的字符替换为int,以便对其求值-Java
我想要一些关于我正在编程的一个与算术级数有关的应用程序的帮助。 基本上,用户输入一个公式——其中包含一个变量n——以及他们想要计算的术语范围。然后,应用程序应计算每种情况,用正在计算的当前项替换n,从第一个项开始直到最后一个项,最后显示完整的序列。 例如,如果我将其作为公式输入: An=将字符串中的字符替换为int,以便对其求值-Java,java,android,replace,eval,Java,Android,Replace,Eval,我想要一些关于我正在编程的一个与算术级数有关的应用程序的帮助。 基本上,用户输入一个公式——其中包含一个变量n——以及他们想要计算的术语范围。然后,应用程序应计算每种情况,用正在计算的当前项替换n,从第一个项开始直到最后一个项,最后显示完整的序列。 例如,如果我将其作为公式输入: An=n+4 在术语范围内: 从1到5 输出应为: A1 = 5.0 //because it is n + 4 and the current term n is 1, so 1 + 2 A2 = 6.0 //n
n+4
在术语范围内:
从1
到5
输出应为:
A1 = 5.0 //because it is n + 4 and the current term n is 1, so 1 + 2
A2 = 6.0 //n + 4, n = 2 so 3 + 2, and so on...
A3 = 7.0
A4 = 8.0
A5 = 9.0 //n reached 5, so the calculation stops there.
Complete sequence: ( 5.0 , 6.0 , 7.0 , 8.0 , 9.0 )
结果被定义为一个双精度
,因为它并不总是一个整数(例如an=n+1/2
)
为了计算数学表达式,我使用,这是一个:
private static double eval(final String str) {
return new Object() {
int pos = -1, ch;
void nextChar() {
ch = (++pos < str.length()) ? str.charAt(pos) : -1;
}
boolean eat(int charToEat) {
while (ch == ' ') nextChar();
if (ch == charToEat) {
nextChar();
return true;
}
return false;
}
double parse() {
nextChar();
double x = parseExpression();
if (pos < str.length()) throw new RuntimeException("Unexpected: " + (char) ch);
return x;
}
// Grammar:
// expression = term | expression `+` term | expression `-` term
// term = factor | term `*` factor | term `/` factor
// factor = `+` factor | `-` factor | `(` expression `)`
// | number | functionName factor | factor `^` factor
double parseExpression() {
double x = parseTerm();
for (; ; ) {
if (eat('+')) x += parseTerm(); // addition
else if (eat('-')) x -= parseTerm(); // subtraction
else return x;
}
}
double parseTerm() {
double x = parseFactor();
for (; ; ) {
if (eat('*')) x *= parseFactor(); // multiplication
else if (eat('/')) x /= parseFactor(); // division
else return x;
}
}
double parseFactor() {
if (eat('+')) return parseFactor(); // unary plus
if (eat('-')) return -parseFactor(); // unary minus
double x;
int startPos = this.pos;
if (eat('(')) { // parentheses
x = parseExpression();
eat(')');
} else if ((ch >= '0' && ch <= '9') || ch == '.') { // numbers
while ((ch >= '0' && ch <= '9') || ch == '.') nextChar();
x = Double.parseDouble(str.substring(startPos, this.pos));
} else if (ch >= 'a' && ch <= 'z') { // functions
while (ch >= 'a' && ch <= 'z') nextChar();
String func = str.substring(startPos, this.pos);
x = parseFactor();
if (func.equals("sqrt")) x = Math.sqrt(x);
else if (func.equals("sin")) x = Math.sin(Math.toRadians(x));
else if (func.equals("cos")) x = Math.cos(Math.toRadians(x));
else if (func.equals("tan")) x = Math.tan(Math.toRadians(x));
else throw new RuntimeException("Unknown function: " + func);
} else {
throw new RuntimeException("Unexpected: " + (char) ch);
}
if (eat('^')) x = Math.pow(x, parseFactor()); // exponentiation
return x;
}
}.parse();
}
但每当我输入一个包含“n”的公式时,应用程序就会崩溃。
日志:
是没有出现的角色。通过单击错误消息,问题似乎来自术语和结果。添加(“A”+i+“=”+eval(strResult)+“\n”)
,这可能是由于将'n'
替换为i造成的
错误消息来自eval()方法-抛出新的RuntimeException(“意外:”+(char)ch)代码>。
我尝试了类似问题的解决方案,但出现了几乎相同的错误。
我是初学者。任何帮助都将不胜感激。请尝试使用此功能。
String strResult = strFormula.replace('n','i').trim().replace(" ","")
将i
的那些值强制转换为字符会导致无法打印的字符。例如,如果i
为1,则生成的字符将成为标题控制字符的开头。这就是为什么你会看到那个有问号的盒子
您需要整数i
的字符串值。只用
strFormula.replace("n", String.valueOf(i))
它仍然崩溃。这只会将字符串中的字符“n”替换为字符“i”(不是它的值,只是它的字符),eval()方法无法计算字符i,因此它不起作用。无论如何,谢谢你java.lang.RuntimeException:Unknown function:i
It worked,应用程序现在正确打印值。非常感谢。
String strResult = strFormula.replace('n','i').trim().replace(" ","")
strFormula.replace("n", String.valueOf(i))