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

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

我现在已根据建议将switch(choices)更改为switch(input),它已删除了我之前收到的错误消息。但是该程序未按预期运行。

它创建一个带有下拉菜单的窗格,将1-5作为选项列出,但是当我选择选项1、2或4时它什么也没做。应该显示来自相应方法的信息。我不确定如何解决此问题。

[当我选择选项3时,它会按原样显示输入框,但是无论输入什么都可以得到相同的结果。我相信这是因为它们被初始化为0,但是我不确定如何在没有数字的情况下将其初始化。

同样,当我选择选项5时,它应该退出程序,但是它不会又一次不知道如何解决此问题。

任何建议?

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 = 0;
                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(input) 
                    {

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

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

                    case "3": 
                    {
                        JOptionPane.showInputDialog(null, "Enter hard disk size in GB: ");
                        double hardDiskSize = 0;
                        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 = 0;
                        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 user-interface switch-statement joptionpane
3个回答
1
投票

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

您的第一个问题,是由于尝试切换字符串数组而不是字符串而引起的。

此:

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;

可以改写为:

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 (input) { // switch on the value you want to check, the one you entered yourself
  case "1":  s.displayDetails(); // there is no need for { and } here, it just adds lines in your code
  break;

现在,这将解决您的原始问题。

下一个问题,在您的while循环中:

while (choice != '5');

选择永远不会是'5',因为您永远不会更新选择的值。一种选择是将其更改为:

while (!"5".equals(input));

因为您已经有了input变量,并且也可以进行比较。

正如我说的,您不会显示完整的代码,因此很难检查所有内容。


0
投票
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) {

在那段代码中,您试图为switch语句使用数组,这是不允许的-而且显然可以理解为什么

您需要了解switch语句正在替换“如果相等”您不能将其分配给多个选项,而是每次选择需要发送到交换机的内容

如果您需要所有它们,只需添加一个循环,然后每次仅发送一个数组项


-1
投票

switch(choices)中更改switch(input)

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