我是一個java初學者。而且我沒有Java中複雜編碼的能力。 我想製作一個程序來解決以字符串形式給出的數學表達式,其中只能包含數字和四個基本的算術運算符。 我已經寫了下面的代碼,我知道的基本知識。但是當我運行代碼時,它顯示錯誤。它說我不能解析對象到雙等...我想解決錯誤的解析成一個數學表達式解決java程序中的雙重
我試圖根據BODMAS規則給予四個基本操作符的優先權。 但我不知道如何用括號括住表達式的括號優先。
下面的代碼對於那些只需要幾行代碼的專家來說似乎很有趣,但我真的是一個初學者。
這裏是我做的代碼: (註釋不寫,因爲這代碼很簡單,任何人都可以理解)
import java.util.*;
import java.util.concurrent.TimeUnit;
class Calculator
{
public static void main(String[] args)
throws Exception
{
int aaa =0;
while(aaa==0){
Scanner sc = new Scanner(System.in);
System.out.println("\nEnter expression to solve : ");
String expression = sc.nextLine();
if(!expression.isEmpty()){
int charlength = expression.length();
String charat0 = ""+expression.charAt(0);
String charatlast = ""+expression.charAt(charlength-1);
ArrayList forops = new ArrayList();
ArrayList forchars = new ArrayList();
if(expression.contains(" ")){
pWD("\nSpaces are not allowed",TimeUnit.MILLISECONDS,30);
}else if(!expression.matches("[-+/*0-9]+")){
pWD("\nEnter only numbers and operators",TimeUnit.MILLISECONDS,30);
}else if(!charat0.matches("[0-9]+")||!charatlast.matches("[0-9]+")){
pWD("first and last characters must be\nnumbers",TimeUnit.MILLISECONDS,30);
}else if(expression.contains("//")||expression.contains("**")||expression.contains("--")||expression.contains("++")||
expression.contains("/+")||expression.contains("*/")||expression.contains("-*")||expression.contains("+-")||
expression.contains("/-")||expression.contains("*+")||expression.contains("-/")||expression.contains("+*")||
expression.contains("/*")||expression.contains("*-")||expression.contains("-+")||expression.contains("+/")){
pWD("There must be SINGLE operator between\ntwo adjacent numbers",TimeUnit.MILLISECONDS,30);
}else{
String[] chararr = expression.split("(?<=[-+*/])|(?=[-+*/])");
int chararrl =chararr.length;
for(int nn=0;nn<chararrl;nn++){
forchars.add(chararr[nn]);
}for(int mn=0;mn<chararrl;mn+=2){
String willparse = ""+forchars.get(mn);
double parseint= Double.parseDouble(willparse);
forchars.set(mn,parseint);
}while(forchars.contains("/")){
int divindex = forchars.indexOf("/");
double prediv = forchars.get(divindex-1);
double nexdiv = forchars.get(divindex+1);
forchars.set(divindex,(prediv/nexdiv));
forchars.remove(forchars.indexOf(prediv));
forchars.remove(forchars.indexOf(nexdiv));
}while(forchars.contains("*")){
int mulindex = forchars.indexOf("*");
double premul = forchars.get(mulindex-1);
double nexmul = forchars.get(mulindex+1);
forchars.set(mulindex,(premul*nexmul));
forchars.remove(forchars.indexOf(premul));
forchars.remove(forchars.indexOf(nexmul));
}while(forchars.contains("-")){
int subindex = forchars.indexOf("-");
double presub = forchars.get(subindex-1);
double nexsub = forchars.get(subindex+1);
forchars.set(subindex,(presub-nexsub));
forchars.remove(forchars.indexOf(presub));
forchars.remove(forchars.indexOf(nexsub));
}while(forchars.contains("+")){
int addindex = forchars.indexOf("+");
double preadd = forchars.get(addindex-1);
double nexadd = forchars.get(addindex+1);
forchars.set(addindex,(preadd+nexadd));
forchars.remove(forchars.indexOf(preadd));
forchars.remove(forchars.indexOf(nexadd));}
double answer1 = forchars.get(0);
String answer = Double.toString(answer1);
pWD("\nFinal answer is : "+answer,TimeUnit.MILLISECONDS,80);
}}else{
pWD("Please enter something",TimeUnit.MILLISECONDS,30);
}}
}public static void pWD(String data, TimeUnit unit, long delay)
throws InterruptedException {
for (char ch:data.toCharArray()) {
System.out.print(ch);
unit.sleep(delay);
}
}
}
請幫我如何解決解析成雙重的錯誤。 如果用戶將表達式括在括號中,請詳細解釋如何解決表達式。
謝謝。
錯誤發生在哪條線上?更妙的是,你能用一個更少的代碼來做一個例子,隔離發生錯誤的地方嗎? –
你應該打印出你的'split'操作的結果。可能有一個子字符串不能轉換爲數字。 –