String with math operators to Integer(带有数学运算符的字符串到整数)
问题描述
我有一个带有数学运算符的字符串,我需要将其转换为 int(答案).
I have a string with math operators which I need to turn into an int
(the answer).
以下代码不起作用,但我不确定如何使 answer 变量起作用.
The below code does not work, but I'm not sure how I can get the answer variable to work.
String question;
int answer;
question = "7/7+9-9*5/5";
answer = Integer.parseInt(question);
推荐答案
Integer.parseInt(question);
...要是这么简单就好了...
Integer.parseInt(question);
... If only it was so simple ...
"7/7+9-9*5/5"="nofollow">中缀表示法 您要评估并将结果打印给用户.这样做的方法是将其转换为 后缀表示法,也称为反向波兰表示法,使用 调车场算法.将表达式转换为后缀评估后,使用此算法
You have there a mathematical expression "7/7+9-9*5/5"
in infix notation which you want to evalutate and print the result to the user. The way to do that is to convert it to postfix notation also known as Reverse Polish notation using the Shunting-yard algorithm. Once you have converted the expression to postfix evaluating is pretty easy using this algorithm
这篇关于带有数学运算符的字符串到整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:带有数学运算符的字符串到整数


基础教程推荐
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 降序排序:Java Map 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01