2014-01-25 54 views
0

所以我正在做一个简单的菜单与选项对话框,但它只是不编译,不知道为什么。试图创建一个菜单,我得到编译器错误

这是错误:

Inventory.java:21: error: illegal start of expression 
public static String promptInventory(String MName, String[] options) 

不知道这里做什么。也是我设置它的方式,它应该循环回菜单,每次都对吗?但我认为它不符合我的目的......

import java.util.ArrayList; 
    import javax.swing.JOptionPane; 
    import javax.swing.JTextArea; 
    import javax.swing.JScrollPane; 

    class Inventory 
    { 
    public static void main(String arg[]) 
    { 
     Database db = new Database(); 
     Database dpl = new Database(); 

    final String[] MENU_OPTIONS = {"exit", "Add product", "Sell product", "Delete product", "Modify product", 
           "Display information"}; 
    final String MENU_NAME = "Inventory"; 


    String selection = promptInventory(MENU_NAME, MENU_OPTIONS); 


    public static String promptInventory(String MName, String[] options) 
    { 
     int selection = JOptionPane.showOptionDialog(null, 
              "Enter your Transaction Type", 
              MName, 
              JOptionPane.DEFAULT_OPTION, 
              JOptionPane.QUESTION_MESSAGE, 
              null, options, options[0]); 
     return (String)options[selection]; 
    } 


    //logic 


     switch (selection) 
     { 
     case "exit" : 
          break; 

     case "Add product" : 
          break; 

     case "Sell product" : 
          break; 

     } 

     String selection = promptInventory(MENU_NAME, MENU_OPTIONS); 

     } 

     } 
+2

移动'promptInventory'你的'main'方法 – Reimeus

+0

你不能在Java中定义函数的其他功能内(除非你声明函数内一个新的类在其中你把另一个功能 - 但这里没用)。 – Njol

+0

你不能在另一个方法中定义一个方法,并且你没有“关闭”main。 –

回答

1

使用一种工具可以为您设置/缩进代码。这使得这些错误显而易见。

您的promptInventory方法现在在主要方法中,这是非法的。

你有方法的类应缩进像

class Inventory 
{ 
    public void method(){ 

    } // end of method 

    public void nextMethod(){ 
     // No methods in here. 
    } 
}// end class 
+0

非常感谢。 – user3235978

相关问题