用java代码生成一个计算器
时间: 2023-03-13 08:10:50 浏览: 90
要生成一个计算器,可以使用Java语言中的Swing类库。例如,可以使用JFrame类创建一个新窗口,使用JPanel类添加按钮,使用JTextField类创建文本框,并使用ActionListener接口为按钮添加动作。
相关问题
用Java生成一个计算器
好的,我可以为您提供一些Java生成计算器的相关资料和代码,您可以参考以下示例:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator extends JFrame implements ActionListener {
private JButton[] buttons;
private JButton clearBtn, backspaceBtn, equalBtn;
private JTextField displayField;
public Calculator() {
super("Calculator");
// Create display field
displayField = new JTextField(20);
displayField.setEditable(false);
displayField.setHorizontalAlignment(JTextField.RIGHT);
// Create buttons
buttons = new JButton[10];
for (int i = 0; i < 10; i++) {
buttons[i] = new JButton(String.valueOf(i));
buttons[i].addActionListener(this);
}
// Create operations buttons
clearBtn = new JButton("C");
clearBtn.addActionListener(this);
backspaceBtn = new JButton("<-");
backspaceBtn.addActionListener(this);
equalBtn = new JButton("=");
equalBtn.addActionListener(this);
// Create layout
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(4, 3, 5, 5));
for (int i = 1; i <= 9; i++) {
buttonPanel.add(buttons[i]);
}
buttonPanel.add(clearBtn);
buttonPanel.add(buttons[0]);
buttonPanel.add(backspaceBtn);
buttonPanel.add(equalBtn);
// Add components
getContentPane().setLayout(new BorderLayout());
getContentPane().add(displayField, BorderLayout.NORTH);
getContentPane().add(buttonPanel, BorderLayout.CENTER);
// Set basic settings
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(250, 250);
setLocationRelativeTo(null);
setResizable(false);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == clearBtn) {
displayField.setText("");
} else if (source == backspaceBtn) {
String text = displayField.getText();
if (text.length() > 0) {
displayField.setText(text.substring(0, text.length() - 1));
}
} else if (source == equalBtn) {
String text = displayField.getText();
if (text.length() > 0) {
try {
// Calculate result
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName("JavaScript");
String result = String.valueOf(engine.eval(text));
displayField.setText(result);
} catch (Exception ex) {
// Handle error
JOptionPane.showMessageDialog(this, "Invalid input", "Error", JOptionPane.ERROR_MESSAGE);
}
}
} else {
JButton button = (JButton) source;
displayField.setText(displayField.getText() + button.getText());
}
}
public static void main(String[] args) {
new Calculator();
}
}
```
此代码是一个基本的Java图形界面计算器,使用了AWT和Swing API,利用ScriptEngineManager API实现简单的计算功能。您可以在此基础上进行更多的功能扩展和优化。希望能对您有所帮助!
用java生成一个计算器代码包括加减乘除乘方和根
在Java中创建一个基本的四则运算计算器,以及支持乘方和开方的功能,可以按照下面的步骤编写:
```java
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("欢迎使用简易计算器!");
while (true) {
System.out.println("\n请选择操作:");
System.out.println("1. 加法");
System.out.println("2. 减法");
System.out.println("3. 乘法");
System.out.println("4. 除法");
System.out.println("5. 幂运算");
System.out.println("6. 开平方");
System.out.print("7. 退出\n");
int choice = scanner.nextInt();
double num1, num2;
if (choice == 7) { // 退出选项
break;
} else if (choice >= 1 && choice <= 4) {
System.out.print("请输入第一个数字:");
num1 = scanner.nextDouble();
System.out.print("请输入第二个数字:");
num2 = scanner.nextDouble();
switch (choice) {
case 1: // 加法
System.out.println(num1 + " + " + num2 + " = " + add(num1, num2));
break;
case 2: // 减法
System.out.println(num1 + " - " + num2 + " = " + subtract(num1, num2));
break;
case 3: // 乘法
System.out.println(num1 + " * " + num2 + " = " + multiply(num1, num2));
break;
case 4: // 除法
if (num2 != 0) {
System.out.println(num1 + " / " + num2 + " = " + divide(num1, num2));
} else {
System.out.println("错误! 除数不能为零.");
}
break;
}
} else if (choice == 5) { // 幂运算
System.out.print("请输入底数:");
num1 = scanner.nextDouble();
System.out.print("请输入指数:");
num2 = scanner.nextDouble();
System.out.println(num1 + " ^ " + num2 + " = " + power(num1, num2));
} else if (choice == 6) { // 开平方
System.out.print("请输入一个非负数:");
num1 = scanner.nextDouble();
if (num1 >= 0) {
System.out.println("sqrt(" + num1 + ") = " + Math.sqrt(num1));
} else {
System.out.println("错误! 只能计算非负数的平方根.");
}
} else {
System.out.println("无效的选择, 请重新输入.");
}
System.out.println();
}
scanner.close();
}
// 数学函数实现
private static double add(double a, double b) {
return a + b;
}
private static double subtract(double a, double b) {
return a - b;
}
private static double multiply(double a, double b) {
return a * b;
}
private static double divide(double a, double b) {
return a / b;
}
private static double power(double base, double exponent) {
return Math.pow(base, exponent);
}
}
```
阅读全文