java 有没有比使用if else语句更有效的方法来设置ActionEvent的不同情况?

mec1mxoz  于 2023-08-01  发布在  Java
关注(0)|答案(2)|浏览(118)

我正在尝试做一个货币兑换程序,我使用一个组合框来列出用户想要进行的转换类型,当用户选择一个选项时,它就会进行转换。有没有比使用一堆if else语句更好的方法来使用equals.IgnoreCase?我为这些选项创建了一个字符串数组,然后用该数组作为参数声明了JComboBox。

@Override
public void actionPerformed(ActionEvent e) {
    String text = textField.getText();
    double c = Double.parseDouble(text);
    if(e.getSource()==comboBox) {
        String choice = comboBox.getSelectedItem().toString();
        if(choice.equalsIgnoreCase("Dollars to Pesos")) { // placeholder text
            System.out.println(choice);
            
        }
        else if(choice.equalsIgnoreCase("Dollars to Pesos")) {
            System.out.println(choice);
            
        }
        else if(choice.equalsIgnoreCase("Pesos to Dollars")) {
            System.out.println(choice);
            
        }
        else if(choice.equalsIgnoreCase("Dollars to GB Pounds")) {
            System.out.println(choice);
            
        }
        else if(choice.equalsIgnoreCase("GB Pounds to Dollars")) {
            System.out.println(choice);
            
        }
        else if(choice.equalsIgnoreCase("Dollars to Yen")) {
            System.out.println(choice);
            
        }
        else if(choice.equalsIgnoreCase("Yen to Dollars")) {
            System.out.println(choice);
            
        }
        
    }
    
}

字符串

2vuwiymt

2vuwiymt1#

  • 我为选项创建了一个字符串数组 * 是的。使用这个数组。假设它被称为possibleChoices,它可能看起来像
private static String[] possibleChoices = {
        "Dollars to Pesos",
        "Pesos to Dollars",
        "Dollars to GB Pounds",
        "GB Pounds to Dollars",
        "Dollars to Yen",
        "Yen to Dollars"
};

@Override
public void actionPerformed(ActionEvent e) {
    String text = textField.getText();
    double c = Double.parseDouble(text);
    if (e.getSource() == comboBox) {
        String choice = comboBox.getSelectedItem().toString();
        if (Arrays.stream(possibleChoices).anyMatch(choice::equalsIgnoreCase)) {
            System.out.println(choice);
        }
    }
}

字符串

mwyxok5s

mwyxok5s2#

Java 8或更高版本可以使用函数接口的Map来创建更灵活的解决方案,如下所示,

Map<String, Runnable> map = new HashMap<>();
map.put("dollars to pesos", () -> System.out.println("Dollars to Pesos"));
map.put("pesos to dollars", () -> System.out.println("Pesos to Dollars"));
map.put("dollars to gb pounds", () -> System.out.println("Dollars to GB Pounds"));
map.put("gb pounds to dollars", () -> System.out.println("GB Pounds to Dollars"));
map.put("dollars to yen", () -> System.out.println("Dollars to Yen"));
map.put("yen to dollars", () -> System.out.println("Yen to Dollars"));

@Override
public void actionPerformed(ActionEvent e) {
    if(e.getSource()==comboBox) {
        String choice = comboBox.getSelectedItem().toString().toLowerCase();
        Runnable runnable = map.get(choice);
        if (runnable != null) {
            runnable.run();
        }
    }
}

字符串
这种方法的另一个好处是,只需在Map中添加或删除条目,就可以轻松地添加或删除货币转换,而不必触及actionPerformed方法中的逻辑。如果您预计转换列表在未来会增长,则它更具可扩展性。

相关问题