問題描述
我有一個帶有數學運算符的字符串,我需要將其轉換為 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
這篇關于帶有數學運算符的字符串到整數的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!