簡體   English   中英

JOptionPane開關語句循環

[英]JOptionPane switch statement loop

@MadProgrammer,切換條件功能在選擇后停止。 我似乎無法將其返回到OnlineStore()。 我嘗試了do-while語句。 沒用 無論輸入1或2,它將恢復為默認值,然后返回菜單。

        import javax.swing.JOptionPane;

        public class OnlineStore 
        {
            String[] ColorType = {"blue", "green", "black"}; 
            final int COLOURS = 3;    // t choices
            int[] Color = new int[COLOURS];
            int sum, mselect;

            public static int display_menu() // Not the main program but the main menu.
            {
                String input;
                int mselect;
                input = JOptionPane.showInputDialog("Welcome!" + "\n\n1. Add tshirt order\n2. Edit tshirt order\n3. View current order\n4. Checkout" + "\n\nPlease enter your choice: ");
                // return Integer.parseInt(input);
                mselect = Integer.parseInt(input);
                return 0;
            }

            public OnlineStore() // Switch-case program
            {
                do 
                {
                display_menu();
                switch (mselect)
                { 
                     case 1:
                        add_t();
                        break;
                    case 2:
                        edit_t();
                        break;
                    default:  // If an error is encountered.
                        JOptionPane.showMessageDialog(null, "Oh dear! Error!");
                        break;
                }
                } while (mselect < 3);
            }

            public final int add_t()
            {  
                for (int index = 0; index < ColorType.length; index++)
                {
        <output>
                }
                return Color.length;
            }

            public final int edit_t() 
            {
                for (int index = 0; index < ColorType.length; index++)
                {
        <output>
            }

            public static void main(String[] args) // Main program
            {
                new OnlineStore(); // Call out the program.
            }

        }

看來您沒有給mselect一個值。 您可能想做的是

        int sum;
        static int mselect = 0;

        public static int display_menu() // Not the main program but the main menu.
        {
            String input;
            input = JOptionPane.showInputDialog("Welcome!" + "\n\n1. Add tshirt order\n2. Edit tshirt order\n3. View current order\n4. Checkout" + "\n\nPlease enter your choice: ");
            // return Integer.parseInt(input);
            mselect = Integer.parseInt(input);
            return 0;
        }

第一...

public static int display_menu() // Not the main program but the main menu.
{
    String input;
    int mselect;
    input = JOptionPane.showInputDialog("Welcome!" + "\n\n1. Add tshirt order\n2. Edit tshirt order\n3. View current order\n4. Checkout" + "\n\nPlease enter your choice: ");
    // return Integer.parseInt(input);
    mselect = Integer.parseInt(input);
    return 0;
}

您沒有返回mselect ,而是返回0 相反,您可能應該做更多類似的事情...

public static int display_menu() // Not the main program but the main menu.
{
    String input;
    input = JOptionPane.showInputDialog("Welcome!" + "\n\n1. Add tshirt order\n2. Edit tshirt order\n3. View current order\n4. Checkout" + "\n\nPlease enter your choice: ");
    // return Integer.parseInt(input);
    return Integer.parseInt(input);
}

接下來,您將使用mselect ,該值沒有值可檢入switch

public OnlineStore() // Switch-case program
{
    do {
        display_menu();
        switch (mselect) {
            case 1:
                add_t();
                break;
            case 2:
                edit_t();
                break;
            default:  // If an error is encountered.
                JOptionPane.showMessageDialog(null, "Oh dear! Error!");
                break;
        }
    } while (mselect < 3);
}

但是,您應該使用display_menu()返回的值

public OnlineStore() // Switch-case program
{
    do {
        switch (display_menu()) {
            case 1:
                add_t();
                break;
            case 2:
                edit_t();
                break;
            default:  // If an error is encountered.
                JOptionPane.showMessageDialog(null, "Oh dear! Error!");
                break;
        }
    } while (mselect < 3);
}

要非常警惕static ,它不是您的朋友,可以輕松地將運行良好的程序轉換為垃圾程序。

相反,您可以做更多類似的事情...

public class OnlineStore {

    String[] ColorType = {"blue", "green", "black"};
    final int COLOURS = 3;    // t choices
    int[] Color = new int[COLOURS];
    int sum;

    public int display_menu() // Not the main program but the main menu.
    {
        String input;
        input = JOptionPane.showInputDialog("Welcome!" + "\n\n1. Add tshirt order\n2. Edit tshirt order\n3. View current order\n4. Checkout" + "\n\nPlease enter your choice: ");
        // return Integer.parseInt(input);
        return Integer.parseInt(input);
    }

    public OnlineStore() // Switch-case program
    {
        boolean exit = false;
        do {
            switch (display_menu()) {
                case 1:
                    add_t();
                    break;
                case 2:
                    edit_t();
                    break;
                case 4:
                    exit = true;
                    break;
                default:  // If an error is encountered.
                    JOptionPane.showMessageDialog(null, "Oh dear! Error!");
                    break;
            }
        } while (!exit);
    }

    public final int add_t() {
        return 0;
    }

    public final int edit_t() {
        return 0;
    }

    public static void main(String[] args) // Main program
    {
        new OnlineStore(); // Call out the program.
    }

}

您正在將輸入分配給局部變量。 其范圍僅與方法有關。 局部變量范圍

mselect = Integer.parseInt(input);
return mselect;

並分配

mselect = display_menu();

或將mselect設置為static並刪除局部變量。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM