简体   繁体   English

限制为数字开关/如果选项?

[英]Limit to the number switch/if options?

Small disclaimer I am new to Java and this is my first real programme i am trying to write. 小免责声明我是Java新手,这是我尝试编写的第一个真实程序。

I am currently writing a programme with 3 class's. 我目前正在编写一个3类的程序。 The first is a GUI with a JComboBox, 4 JButtons and 20 JTextFields. 第一个是带有JComboBox,4个JButton和20个JTextField的GUI。

The second takes information from the JComboBox box and uses it to give labels to the 4 JButtons using a set of if statements. 第二个从JComboBox框中获取信息,并使用它通过一组if语句为4个JButton提供标签。

The third populates the JTextFields when one of the the JButtons is clicked depending on the button clicked and the choice in the JComboBox using a set of switch statements. 当单击一个JButton时,第三个将填充JTextField,具体取决于单击的按钮以及使用一组switch语句在JComboBox中的选择。

The first 2 class's work fine and the third works fine until i enter x amount of switches and then i start to get an error. 直到我输入x数量的开关,然后开始出现错误,前两个类的工作正常,而第三个类工作正常。

A small example of my code for the third class is 我的第三类代码的一个小例子是

switch (hiddenText) {
        case "Abecean Longfin":
        if (command.equals("Weakness to Frost")){
        gui.r1.setText("Elves Ear");
        gui.r2.setText("Fire Salts");
        gui.r3.setText("Ice Wraith Teeth");
        gui.r4.setText("White Cap");
        gui.r5.setText("");
        gui.r6.setText("");
        gui.r7.setText("");
        gui.r8.setText("");
        gui.r9.setText("");
        gui.r10.setText("");
        gui.r11.setText("");
        gui.r12.setText("");
        gui.r13.setText("");
        gui.r14.setText("");
        gui.r15.setText("");
        gui.r16.setText("");
        gui.r17.setText("");
        gui.r18.setText("");
        gui.r19.setText("");
        gui.r20.setText("");

        }
        if (command.equals("Fortify Sneak")){
        gui.r1.setText("Beehive Husk");
        gui.r2.setText("Frost Mirriam");
        gui.r3.setText("Hawk Feathers");
        gui.r4.setText("Human Flesh");
        gui.r5.setText("Powdered Mammoth Tusk");
        gui.r6.setText("Purple Mountain Flower");
        gui.r7.setText("");
        gui.r8.setText("");
        gui.r9.setText("");
        gui.r10.setText("");
        gui.r11.setText("");
        gui.r12.setText("");
        gui.r13.setText("");
        gui.r14.setText("");
        gui.r15.setText("");
        gui.r16.setText("");
        gui.r17.setText("");
        gui.r18.setText("");
        gui.r19.setText("");
        gui.r20.setText("");
        }
        if (command.equals("Weakness to Poison")){
        gui.r1.setText("Bleeding Crown");
        gui.r2.setText("Chaurus Eggs");
        gui.r3.setText("Deathbell");
        gui.r4.setText("Giant Lichen");
        gui.r5.setText("Pine Thrush Egg");
        gui.r6.setText("Sabre Cat Tooth");
        gui.r7.setText("Small Antlers");
        gui.r8.setText("");
        gui.r9.setText("");
        gui.r10.setText("");
        gui.r11.setText("");
        gui.r12.setText("");
        gui.r13.setText("");
        gui.r14.setText("");
        gui.r15.setText("");
        gui.r16.setText("");
        gui.r17.setText("");
        gui.r18.setText("");
        gui.r19.setText("");
        gui.r20.setText("");
        }
        if (command.equals("Fortify Restoration")){
        gui.r1.setText("Cyrodilic Spadetail");
        gui.r2.setText("Salt Pile");
        gui.r3.setText("Small Antlers");
        gui.r4.setText("Small Pearl");
        gui.r5.setText("");
        gui.r6.setText("");
        gui.r7.setText("");
        gui.r8.setText("");
        gui.r9.setText("");
        gui.r10.setText("");
        gui.r11.setText("");
        gui.r12.setText("");
        gui.r13.setText("");
        gui.r14.setText("");
        gui.r15.setText("");
        gui.r16.setText("");
        gui.r17.setText("");
        gui.r18.setText("");
        gui.r19.setText("");
        gui.r20.setText("");
        }
        break;

There are a total of 92 cases, each with 4 if statements and the programme works fine until i get to 57 cases and 2 if statements then i get an error symbol in the class tab title in IDE (using NetBeans) but not within the code itself. 总共有92种情况,每种情况都有4条if语句,并且程序可以正常工作,直到我达到57种情况和2条if语句,然后我在IDE中的类选项卡标题(使用NetBeans)中得到了一个错误符号,但不在代码内本身。

When i compile and run the code i get an error box appearing saying "One or more projects compiled with errors" but if i choose to run anyway the programme will run fine as far as i have seen. 当我编译并运行代码时,出现一个错误框,显示“一个或多个错误编译的项目”,但是如果我选择运行,则该程序可以正常运行。

I have tried writing the code in various ways.eg originally using if statements and originally having class 2 and 3 as the same class and i always get this error at reaching x amount of choices. 我试图以各种方式编写代码。例如最初使用if语句,最初将类2和3作为同一类,并且在达到x的选择量时我总是会收到此错误。

In previous versions i would sometimes get the error symbol appear in the class tab title in the IDE and not within the code as usual but when trying to run the programme it wouldn 't run at all and gave some classpath error which i can't seem to reproduce now so can't post the exact code. 在以前的版本中,有时我会在IDE的类选项卡标题中出现错误符号,而不是像往常一样不在代码内显示,但是当尝试运行该程序时,它根本不会运行,并给出了一些我无法执行的类路径错误似乎现在可以重现,因此无法发布确切的代码。

From the testing i have done.eg removing different sections of code it appears that it happens when x amount of choices are added.ie i can add the 52nd case and 3rd if statement and the error appears but if i remove a previous if statement so there are 52 cases and 2 if statements still then everything if fine. 从测试中我已经完成了。例如删除代码的不同部分,似乎在添加x个选择项时发生。例如,我可以添加第52种情况和第3种if语句,并且会出现错误,但是如果我删除了先前的if语句,那么有52种情况,还有2条if语句,则一切正常。

From experimenting it would seem there is a limit to the amount of switches/if statements i can use although from looking around i could in theory have an infinite amount? 从实验上看,我可以使用的switch / if语句数量似乎是有限的,尽管从理论上看,我可以无限量吗?

So my question is, is there a limit to the amount of switches/if statements i can use or is there something else going on which is causing an error when i enter x number of switches/if statements? 所以我的问题是,我可以使用的switch / if语句数量是否有限制?或者是否有其他事情在我输入x数量的switch / if语句时引起错误?

you need to separate your data from your code. 您需要将数据与代码分开。 stick all that data in a combination of maps and lists and use some simple, common code to update the gui elements. 将所有数据粘贴到地图和列表的组合中,并使用一些简单的通用代码来更新gui元素。

// setup data code
List<String> elements = Arrays.asList("Elves Ear", "Fire Salts", ...);
Map<String,List<String>> elementMap;
elementMap.put("Weakness to Frost");

// setup ui code
List<String> elements = elementMap.get(command);
initUI(elements);

public void initUI(List<String> elements) {
  gui.r1.setText(elements.get(0));
  // ...
}

note, you could keep all your text fields in a List, and then the update code is: 注意,您可以将所有文本字段保留在一个列表中,然后更新代码为:

public void initUI(List<String> elements) {
  for(int i = 0; i< textFields.size(); ++i) {
    textFields.get(i).setText(elements.get(i));
  }
}

in general, if you find yourself writing a lot of repetitive code, you are probably doing it wrong. 通常,如果您发现自己编写了很多重复的代码,则可能做错了。

also, as @thatidiotguy pointed out in the comments, as you advance, you could move the data out of code like my example and into some sort of separate config file. 同样,正如@thatidiotguy在注释中指出的那样,随着前进,您可以将数据从代码中移出,例如我的示例,并移到某种单独的配置文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM