如何计算两个整数值的结果,但从java中的jComboBox获取加法或乘法运算符

如何计算两个整数值的结果,但从java中的jComboBox获取加法或乘法运算符,java,swing,jcombobox,Java,Swing,Jcombobox,假设我有整数变量a,b和c c = a + b; or c = a - b; or c = a / b; or c = a * b; 如您所见,计算运算符需要在运行时动态传递。因此,我有一个由运算符组成的jComboBox,因此用户将从jComboBox中选择+、-、*或/ 如何获取jCombobox selectedItem(可以是/、*、-、或+),并使用它获取c的值 例如,如果用户选择*,则表达式应为c=a*b,否则如果用户选择说+,则表达式应为c=a+b您必须获得JComboBox的值

假设我有整数变量a,b和c

c = a + b; or c = a - b; or c = a / b; or c = a * b;
如您所见,计算运算符需要在运行时动态传递。因此,我有一个由运算符组成的jComboBox,因此用户将从jComboBox中选择+、-、*或/

如何获取jCombobox selectedItem(可以是/、*、-、或+),并使用它获取c的值


例如,如果用户选择*,则表达式应为c=a*b,否则如果用户选择说+,则表达式应为c=a+b

您必须获得
JComboBox
的值,并对字符执行
switch
语句(因为不能对字符串执行
switch
)要查看它是哪个,请执行正确的操作


编辑:字符串上的超高<代码>开关。。。。但是(Java 7)

您必须获取
JComboBox
的值,并对Char执行
switch
语句(因为字符串上不能
switch
),以查看它是哪个,然后执行正确的操作

编辑:字符串上的超高<代码>开关。。。。但是(Java7)

可以帮助您

或者,您可以将组合框中显示的每个运算符与自定义
计算器
接口的实现相关联,该接口为您提供任意两个数字的结果。比如:

interface Calculator {
  public int calculate(int a, int b);
}
class AddCalculator implements Calculator {
  public int calculate(int a, int b) {return a+b;}
}
关联可以是
HashMap
的形式。接口
计算器
可以是作为参数传递并作为结果返回的类型的泛型。这将是我解决问题的方法,但我相信可能有一种更简单的方法。

会有所帮助

int c = compute(a,b, (String)comboBox.getSelectedItem()); 
或者,您可以将组合框中显示的每个运算符与自定义
计算器
接口的实现相关联,该接口为您提供任意两个数字的结果。比如:

interface Calculator {
  public int calculate(int a, int b);
}
class AddCalculator implements Calculator {
  public int calculate(int a, int b) {return a+b;}
}
关联可以是
HashMap
的形式。接口
计算器
可以是作为参数传递并作为结果返回的类型的泛型。这将是我解决问题的方法,但我相信可能会有更简单的方法

int c = compute(a,b, (String)comboBox.getSelectedItem()); 

}


}这里有一种“欺骗”的方法。所有的解析都是由
脚本引擎
完成的,我们只需要组装表达式的各个部分

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.script.*;

class ScriptEngineCalculations {

    public static void main(String[] args) {
        final ScriptEngine engine = new ScriptEngineManager().
            getEngineByExtension( "js" );

        String[] ops = {"+", "-", "*", "/"};

        JPanel gui = new JPanel(new BorderLayout(2,2));
        JPanel labels = new JPanel(new GridLayout(0,1));
        gui.add(labels, BorderLayout.WEST);
        labels.add(new JLabel("a"));
        labels.add(new JLabel("operand"));
        labels.add(new JLabel("b"));
        labels.add(new JLabel("="));

        JPanel controls = new JPanel(new GridLayout(0,1));
        gui.add(controls, BorderLayout.CENTER);
        final JTextField a = new JTextField(10);
        controls.add(a);
        final JComboBox operand = new JComboBox(ops);
        controls.add(operand);
        final JTextField b = new JTextField(10);
        controls.add(b);
        final JTextField output = new JTextField(10);
        controls.add(output);

        ActionListener al = new ActionListener(){
            public void actionPerformed(ActionEvent ae) {
                String expression =
                    a.getText() +
                    operand.getSelectedItem() +
                    b.getText();
                try {
                    Object result = engine.eval(expression);
                    if (result==null) {
                        output.setText( "Output was 'null'" );
                    } else {
                        output.setText( result.toString() );
                    }
                } catch(ScriptException se) {
                    output.setText( se.getMessage() );
                }
            }
        };

        // do the calculation on event.
        operand.addActionListener(al);
        a.addActionListener(al);
        b.addActionListener(al);

        JOptionPane.showMessageDialog(null, gui);
    }
}

另见
  • 我的任务是探索JS引擎的功能

    • 这里有一种“欺骗”的方法。所有的解析都是由
      脚本引擎
      完成的,我们只需要组装表达式的各个部分

      import java.awt.*;
      import java.awt.event.*;
      import javax.swing.*;
      import javax.script.*;
      
      class ScriptEngineCalculations {
      
          public static void main(String[] args) {
              final ScriptEngine engine = new ScriptEngineManager().
                  getEngineByExtension( "js" );
      
              String[] ops = {"+", "-", "*", "/"};
      
              JPanel gui = new JPanel(new BorderLayout(2,2));
              JPanel labels = new JPanel(new GridLayout(0,1));
              gui.add(labels, BorderLayout.WEST);
              labels.add(new JLabel("a"));
              labels.add(new JLabel("operand"));
              labels.add(new JLabel("b"));
              labels.add(new JLabel("="));
      
              JPanel controls = new JPanel(new GridLayout(0,1));
              gui.add(controls, BorderLayout.CENTER);
              final JTextField a = new JTextField(10);
              controls.add(a);
              final JComboBox operand = new JComboBox(ops);
              controls.add(operand);
              final JTextField b = new JTextField(10);
              controls.add(b);
              final JTextField output = new JTextField(10);
              controls.add(output);
      
              ActionListener al = new ActionListener(){
                  public void actionPerformed(ActionEvent ae) {
                      String expression =
                          a.getText() +
                          operand.getSelectedItem() +
                          b.getText();
                      try {
                          Object result = engine.eval(expression);
                          if (result==null) {
                              output.setText( "Output was 'null'" );
                          } else {
                              output.setText( result.toString() );
                          }
                      } catch(ScriptException se) {
                          output.setText( se.getMessage() );
                      }
                  }
              };
      
              // do the calculation on event.
              operand.addActionListener(al);
              a.addActionListener(al);
              b.addActionListener(al);
      
              JOptionPane.showMessageDialog(null, gui);
          }
      }
      

      另见
      • 我的任务是探索JS引擎的功能

      请注意,您还不能在
      字符串上切换
      !Java 7终于引入了这个特性。请注意,您还不能在
      字符串上
      切换
      !Java 7终于引入了这个特性。类似的问题可能对这个有点过分了,但是对的+1。类似的问题可能对这个有点过分了,但是对的+1。