2016-04-09 154 views
-3

我似乎弄清楚不能爲什麼我收到此錯誤錯誤:「)」預期編譯器錯誤

Math.java:6: error: ')' expected 
     if(args[1].equalsIgnoreCase("+") 
             ^
Math.java:11: error: ')' expected 
       else if(args[1].equalsIgnoreCase("x") 
                ^
Math.java:16: error: ')' expected 
       else if(args[1].equalsIgnoreCase("-") 
                ^
Math.java:21: error: ')' expected 
       else if(args[1].equalsIgnoreCase("/") 
                ^
4 errors 

我的代碼是

class Math 
{ 
    public static void main(String args[]) 
    { 
     if(args[1].equalsIgnoreCase("+") 
     { 
     sum = Integer.parseInt(args[0]) + Integer.parseInt(args[2]); 
     System.out.println("The answere is : " + sum); 
     } 
     else if(args[1].equalsIgnoreCase("x") 
     { 
     sum = Integer.parseInt(args[0]) * Integer.parseInt(args[2]); 
     System.out.println("The answere is : " + sum); 
     } 
     else if(args[1].equalsIgnoreCase("-") 
     { 
     sum = Integer.parseInt(args[0]) - Integer.parseInt(args[2]); 
     System.out.println("The answere is : " + sum); 
     } 
     else if(args[1].equalsIgnoreCase("/") 
     { 
     sum = Integer.parseInt(args[0])/Integer.parseInt(args[2]); 
     System.out.println("The answere is : " + sum); 
     } 
     else 
     { 
     System.out.println("Something seems to be wrong, please try again."); 
     } 

    } 
} 

當我試圖進入封閉parenthesizes地方說他們應該去,它會得到更多的錯誤。任何人都可以給我一個簡要的描述是什麼導致這個錯誤。我只是試圖根據程序運行時輸入到命令行的參數來製作一個數學程序。

例如,如果我輸入「java數學1 + 1」它會解決這個問題,並吐出「答案是2」。

任何幫助你們可以提供這個將不勝感激。

+1

你在每一個這樣的線都缺少')'。添加那些給出*其他*錯誤的事實並不意味着他們不應該在那裏。 – Biffen

+2

你沒有在任何地方定義'sum'。 – SomeJavaGuy

回答

0

有兩個錯誤,你沒有定義一個地方的總和,以及你在每個條件下缺少一個paranthesis。

public static void main(String args[]) 
{ 
    int sum = 0; // you had do define sum 
    if(args[1].equalsIgnoreCase("+")) // one closing missing 
    { 
     sum = Integer.parseInt(args[0]) + Integer.parseInt(args[2]); 
     System.out.println("The answere is : " + sum); 
    } 
    else if(args[1].equalsIgnoreCase("x")) // one closing missing 
    { 
    sum = Integer.parseInt(args[0]) * Integer.parseInt(args[2]); 
    System.out.println("The answere is : " + sum); 
    } 
    else if(args[1].equalsIgnoreCase("-")) // one closing missing 
    { 
    sum = Integer.parseInt(args[0]) - Integer.parseInt(args[2]); 
    System.out.println("The answere is : " + sum); 
    } 
    else if(args[1].equalsIgnoreCase("/")) // one closing missing 
    { 
    sum = Integer.parseInt(args[0])/Integer.parseInt(args[2]); 
    System.out.println("The answere is : " + sum); 
    } 
    else 
    { 
    System.out.println("Something seems to be wrong, please try again."); 
    } 

} 
0

好吧,讓我們一步一步來。預期錯誤「)」就是爲什麼你忘了關的,如果「(」:

if(args[1].equalsIgnoreCase("+")) 

...等等,每個是否 加,你沒有在任何地方定義和。這是另一個錯誤,你會遇到。

2

您要爲每個if條件錯過)以及你沒有定義sum

class Math 
{ 
    public static void main(String args[]) 
    { 
     int sum; 
     if(args[1].equalsIgnoreCase("+")) 
     { 
     sum = Integer.parseInt(args[0]) + Integer.parseInt(args[2]); 
     System.out.println("The answere is : " + sum); 
     } 
     else if(args[1].equalsIgnoreCase("x")) 
     { 
     sum = Integer.parseInt(args[0]) * Integer.parseInt(args[2]); 
     System.out.println("The answere is : " + sum); 
     } 
     else if(args[1].equalsIgnoreCase("-")) 
     { 
     sum = Integer.parseInt(args[0]) - Integer.parseInt(args[2]); 
     System.out.println("The answere is : " + sum); 
     } 
     else if(args[1].equalsIgnoreCase("/")) 
     { 
     sum = Integer.parseInt(args[0])/Integer.parseInt(args[2]); 
     System.out.println("The answere is : " + sum); 
     } 
     else 
     { 
     System.out.println("Something seems to be wrong, please try again."); 
     } 
    } 
} 
0
class Math { 
public static void main(String args[]) 
{ 
    if(args[1].equalsIgnoreCase("+")) 
    { 
    sum = Integer.parseInt(args[0]) + Integer.parseInt(args[2]); 
    System.out.println("The answere is : " + sum); 
    } 
    else if(args[1].equalsIgnoreCase("x")) 
    { 
    sum = Integer.parseInt(args[0]) * Integer.parseInt(args[2]); 
    System.out.println("The answere is : " + sum); 
    } 
    else if(args[1].equalsIgnoreCase("-")) 
    { 
    sum = Integer.parseInt(args[0]) - Integer.parseInt(args[2]); 
    System.out.println("The answere is : " + sum); 
    } 
    else if(args[1].equalsIgnoreCase("/")) 
    { 
    sum = Integer.parseInt(args[0])/Integer.parseInt(args[2]); 
    System.out.println("The answere is : " + sum); 
    } 
    else 
    { 
    System.out.println("Something seems to be wrong, please try again."); 
    } 

} } 

這應該可以解決您的問題。你需要確保每個支架都關閉。我認爲你需要使用更好的IDE。

1

you are not completing if() statement complete if statement and use this for compare

if(Character.toLowerCase(arg[1])==Character.toLowerCase('+')){ 
} 

,或者您可以使用開關()方法