可以用java使用列表框、组合框和菜单组件制作一个算术运算器示例吗。要求可以输入五个数据,第一行为初始数据,下面四行为均可输入数据并在数据左边可自由选择“+-*/”四个符号来参与运算,最后一行用“=”符号来运算结果。四行运算符没有优 先级,按照顺序运算。增加要求:1.采用文本行数组,操作数个数为5个。2.解决除数为 0的问题3.区分“/”为整数除或实数除
时间: 2024-02-22 16:58:39 浏览: 26
可以使用Java的Swing库来创建一个基于列表框、组合框和菜单组件的算术运算器示例。以下是一个简单的实现:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ArithmeticCalculator extends JFrame implements ActionListener {
private JTextField[] operandFields;
private JComboBox<String>[] operatorBoxes;
private JTextField resultField;
private JButton calculateButton;
private JRadioButton intDivRadio, realDivRadio;
public ArithmeticCalculator() {
super("Arithmetic Calculator");
// Create operand text fields
operandFields = new JTextField[5];
for (int i = 0; i < operandFields.length; i++) {
operandFields[i] = new JTextField(5);
}
// Create operator combo boxes
operatorBoxes = new JComboBox[4];
String[] operators = {"+", "-", "*", "/"};
for (int i = 0; i < operatorBoxes.length; i++) {
operatorBoxes[i] = new JComboBox<String>(operators);
}
// Create result text field and calculate button
resultField = new JTextField(10);
resultField.setEditable(false);
calculateButton = new JButton("Calculate");
calculateButton.addActionListener(this);
// Create radio buttons for integer or real division
intDivRadio = new JRadioButton("Integer Division");
realDivRadio = new JRadioButton("Real Division");
ButtonGroup divGroup = new ButtonGroup();
divGroup.add(intDivRadio);
divGroup.add(realDivRadio);
intDivRadio.setSelected(true);
// Create panel for input fields
JPanel inputPanel = new JPanel(new GridLayout(5, 3));
for (int i = 0; i < 4; i++) {
inputPanel.add(operandFields[i]);
inputPanel.add(operatorBoxes[i]);
}
inputPanel.add(operandFields[4]);
inputPanel.add(new JLabel("="));
inputPanel.add(resultField);
// Create panel for division radio buttons
JPanel divPanel = new JPanel();
divPanel.add(intDivRadio);
divPanel.add(realDivRadio);
// Create panel for calculate button
JPanel buttonPanel = new JPanel();
buttonPanel.add(calculateButton);
// Add panels to main frame
setLayout(new BorderLayout());
add(inputPanel, BorderLayout.CENTER);
add(divPanel, BorderLayout.NORTH);
add(buttonPanel, BorderLayout.SOUTH);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
// Get operands and operators from input fields
double[] operands = new double[5];
for (int i = 0; i < operandFields.length; i++) {
String operandStr = operandFields[i].getText();
if (operandStr.isEmpty()) {
JOptionPane.showMessageDialog(this, "Please enter all operands.");
return;
}
try {
operands[i] = Double.parseDouble(operandStr);
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(this, "Invalid operand: " + operandStr);
return;
}
}
char[] operators = new char[4];
for (int i = 0; i < operatorBoxes.length; i++) {
operators[i] = ((String)operatorBoxes[i].getSelectedItem()).charAt(0);
}
// Calculate result
double result = operands[0];
for (int i = 0; i < operators.length; i++) {
switch (operators[i]) {
case '+':
result += operands[i+1];
break;
case '-':
result -= operands[i+1];
break;
case '*':
result *= operands[i+1];
break;
case '/':
if (operands[i+1] == 0) {
JOptionPane.showMessageDialog(this, "Cannot divide by zero.");
return;
}
if (intDivRadio.isSelected()) {
result /= operands[i+1];
result = (int)result;
} else {
result /= operands[i+1];
}
break;
}
}
// Update result field
resultField.setText(Double.toString(result));
}
public static void main(String[] args) {
ArithmeticCalculator calc = new ArithmeticCalculator();
calc.setVisible(true);
}
}
```
此代码使用了文本行数组来存储输入的操作数,并使用组合框来选择运算符。处理除数为零的情况,并提供了整数除和实数除的选项。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)