From 3b0afb4bdd785855fcd014aa2780ded2c1345031 Mon Sep 17 00:00:00 2001 From: chm <3227492039@qq.com> Date: Thu, 17 Oct 2024 16:20:28 +0800 Subject: [PATCH] 2 --- Calculator.java | 72 ----------------------------- SimpleCalculator.java | 105 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 105 insertions(+), 72 deletions(-) delete mode 100644 Calculator.java create mode 100644 SimpleCalculator.java diff --git a/Calculator.java b/Calculator.java deleted file mode 100644 index af20010..0000000 --- a/Calculator.java +++ /dev/null @@ -1,72 +0,0 @@ -import java.util.Scanner; - -public class Calculator { - - // 加法 - public double add(double a, double b) { - return a + b; - } - - // 减法 - public double subtract(double a, double b) { - return a - b; - } - - // 乘法 - public double multiply(double a, double b) { - return a * b; - } - - // 除法 - public double divide(double a, double b) { - if (b == 0) { - throw new IllegalArgumentException("Denominator cannot be zero."); - } - return a / b; - } - - // 取余 - public double remainder(double a, double b) { - return a % b; - } - - public static void main(String[] args) { - Scanner scanner = new Scanner(System.in); - Calculator calc = new Calculator(); - - System.out.println("Enter first number:"); - double num1 = scanner.nextDouble(); - - System.out.println("Enter second number:"); - double num2 = scanner.nextDouble(); - - System.out.println("Choose operation (+, -, *, /, %):"); - char operation = scanner.next().charAt(0); - - double result; - switch (operation) { - case '+': - result = calc.add(num1, num2); - break; - case '-': - result = calc.subtract(num1, num2); - break; - case '*': - result = calc.multiply(num1, num2); - break; - case '/': - result = calc.divide(num1, num2); - break; - case '%': - result = calc.remainder(num1, num2); - break; - default: - System.out.println("Invalid operation"); - scanner.close(); - return; - } - - System.out.println("Result: " + result); - scanner.close(); - } -} \ No newline at end of file diff --git a/SimpleCalculator.java b/SimpleCalculator.java new file mode 100644 index 0000000..7e616a5 --- /dev/null +++ b/SimpleCalculator.java @@ -0,0 +1,105 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +public class SimpleCalculator extends JFrame { + + private JTextField display; + private JPanel panel; + private double operand1 = 0; + private double operand2 = 0; + private char operator; + private boolean userIsTypingSecondNumber = false; + + public SimpleCalculator() { + setTitle("Simple Calculator"); + setSize(400, 500); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setLocationRelativeTo(null); + + // Display text field + display = new JTextField(); + display.setEditable(false); + display.setHorizontalAlignment(SwingConstants.RIGHT); + add(display, BorderLayout.NORTH); + + // Panel for buttons + panel = new JPanel(); + panel.setLayout(new GridLayout(5, 4, 10, 10)); + + // Add numbers and operations to panel + String[] buttons = { + "7", "8", "9", "/", + "4", "5", "6", "*", + "1", "2", "3", "-", + "0", ".", "=", "+", + "C" + }; + + for (String text : buttons) { + JButton button = new JButton(text); + button.setFont(new Font("Verdana", Font.PLAIN, 24)); + button.setFocusable(false); + panel.add(button); + button.addActionListener(new ButtonClickListener()); + } + + add(panel, BorderLayout.CENTER); + } + + private class ButtonClickListener implements ActionListener { + @Override + public void actionPerformed(ActionEvent e) { + String command = e.getActionCommand(); + + if ("0123456789.".contains(command)) { + display.setText(display.getText().concat(command)); + } else if ("+-*/".contains(command)) { + operand1 = Double.parseDouble(display.getText()); + operator = command.charAt(0); + userIsTypingSecondNumber = true; + display.setText(""); // Clear the display for the next number + } else if (command.equals("=")) { + if (userIsTypingSecondNumber) { + operand2 = Double.parseDouble(display.getText()); + double result = 0; + switch (operator) { + case '+': + result = operand1 + operand2; + break; + case '-': + result = operand1 - operand2; + break; + case '*': + result = operand1 * operand2; + break; + case '/': + if (operand2 != 0) { + result = operand1 / operand2; + } else { + display.setText("Error"); + return; + } + break; + } + display.setText(String.valueOf(result)); + operand1 = result; // Store the result for possible chain operations + userIsTypingSecondNumber = false; // Reset for next operation + } + } else if (command.equals("C")) { + display.setText(""); + operand1 = 0; + operand2 = 0; + userIsTypingSecondNumber = false; + } + } + } + + public static void main(String[] args) { + SwingUtilities.invokeLater(() -> { + SimpleCalculator calculator = new SimpleCalculator(); + calculator.setVisible(true); + }); + } +} \ No newline at end of file