错误消息:“无法打开String []类型的值。仅允许使用可转换的int值,字符串或枚举变量”

问题描述 投票:0回答:2

[我正在尝试通过switch语句使用JOptionPane创建GUI菜单,但出现此错误消息:“无法打开String []类型的值。仅允许使用可转换的int值,String或枚举变量”。

我需要采取什么措施来纠正此错误,我似乎在任何地方都找不到任何东西。

这是我的代码,错误消息显示在线路开关(选择)上

import javax.swing.JOptionPane;

public class SystemTestGUI_Y3881268 {

    public static void main(String[] args) {
        System_Y3881268 s = new System_Y3881268("Lenovo", "Ideacentre A340-24IWL", 2);
        s.setHardDisk(2);
        s.setMemory(128);
        s.setPurchaseCost(599);

        s.displayDetails();
        s.diagnoseSystem();
        System_Y3881268.displaySystemProperties();

        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createGUI();
            }

            private void createGUI() {

                char choice;
                do {
                    String[] choices = { "Select...", "1", "2", "3", "4", "5" };
                    String input = (String) JOptionPane.showInputDialog(null,
                            "Select a choice\n1: Print System Details\n2: Diagnose "
                                    + "System\n3: Set Details\n4: Print System Properties\n5: Quit the Program",
                            "Computer System Menu", JOptionPane.INFORMATION_MESSAGE, null, choices, choices[0]);

                    switch (choices) {

                    case "1": {
                        s.displayDetails();
                    }
                        break;

                    case "2": {
                        s.diagnoseSystem();
                    }
                        break;

                    case "3": {
                        JOptionPane.showInputDialog(null, "Enter hard disk size in GB: ");
                        double hardDiskSize;
                        s.setHardDisk(hardDiskSize);
                        if (hardDiskSize < 2) {
                            JOptionPane.showMessageDialog(null, "Hard disk size = Low");
                        }

                        else {
                            JOptionPane.showMessageDialog(null, "Hard disk size = Ok");
                        }

                        JOptionPane.showInputDialog(null, "Enter memory size in MB: ");
                        int memorySize;
                        s.setMemory(memorySize);
                        if (memorySize < 128) {
                            JOptionPane.showMessageDialog(null, "Memory Ok = False");
                        }

                        else {
                            JOptionPane.showMessageDialog(null, "Memory Ok = True");
                        }
                    }
                        break;

                    case "4": {
                        System_Y3881268.displaySystemProperties();
                    }
                        break;

                    case "5":
                        break;
                    default:
                        JOptionPane.showMessageDialog(null, "Enter only numbers from 1 - 5");

                    }
                } while (choice != '5');

            }
        });
    }
}
java compiler-errors switch-statement
2个回答
0
投票

一些修复程序,但是,由于您没有显示完整的代码,因此它并不完整。


-1
投票

switch(choices)中更改switch(input)

© www.soinside.com 2019 - 2024. All rights reserved.