2015-01-05 32 views
2

以下一些Java課程初學者水平,到了這個問題:JOptionPane.showMessageDialog無效AnnotationName

Multiple markers at this line 
    - Syntax error on token "showMessageDialog", invalid 
    AnnotationName 
    - Syntax error on token "(", { expected after this token 
    - Syntax error on tokens, ConstructorHeaderName expected instead 

而行是:

JOptionPane.showMessageDialog(null, "The answer is " +sum, "The Title", JOptionPane.INFORMATION_MESSAGE); 

Java文檔說,這個方法接受4參數,可以讓我我正在發送4個參數。此外,我已導入所需的庫:

import javax.swing.JOptionPane; 

JOptionPane.showInputDialog工程確定,但不JOptionPane.showMessageDialog。我錯過了什麼? 謝謝。


全碼

package java_practice; 
import javax.swing.JOptionPane; 

public class GraphicalUserInterface { 
    String first_number = JOptionPane.showInputDialog("Enter first number"); 
    String second_number = JOptionPane.showInputDialog("Enter second number"); 

    int num1 = Integer.parseInt(first_number); 
    int num2 = Integer.parseInt(second_number); 
    int sum = num1 + num2; 

    JOptionPane.showMessageDialog(null, "The answer is " +sum, "The Title", JOptionPane.INFORMATION_MESSAGE); 
} 
+0

報告錯誤的人之前是什麼......? – Adam

+0

@亞當我會把代碼.. 1時刻。 – rmagnum2002

+1

是你的代碼.do裏面有一個方法 –

回答

5

你已經錯過了方法聲明。您的代碼位於類的主體內,這會令編譯器/ IDE混淆,例如關於無效註釋的錯誤等。

需要在某種形式,塊或靜態塊的方法內。

public class GraphicalUserInterface { 
    public static void main(String [] args) { // <========= 

     String first_number = JOptionPane.showInputDialog("Enter first number"); 
     String second_number = JOptionPane.showInputDialog("Enter second number"); 

     int num1 = Integer.parseInt(first_number); 
     int num2 = Integer.parseInt(second_number); 
     int sum = num1 + num2; 

     JOptionPane.showMessageDialog(null, "The answer is " +sum, "The Title", JOptionPane.INFORMATION_MESSAGE); 

    } // <========= 
} 
+0

錯過了主塊.. OMG ... noob ..謝謝。 +1,我會在5分鐘內接受它。 ;) – rmagnum2002

+0

不用擔心,我們都在那裏:) – Adam

相關問題