2025.3.20(周四)

找零的代码:

package back;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Back {
    public static void main(String[] args) {
        // 创建主窗口
        JFrame frame = new JFrame("找零计算器");
        frame.setSize(400, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(null);

        // 创建商品价格标签和输入框
        JLabel labelPrice = new JLabel("请输入商品价格:");
        labelPrice.setBounds(50, 30, 120, 25);
        frame.add(labelPrice);

        JTextField textPrice = new JTextField();
        textPrice.setBounds(180, 30, 150, 25);
        frame.add(textPrice);

        // 创建支付金额标签和输入框
        JLabel labelPay = new JLabel("请输入支付金额:");
        labelPay.setBounds(50, 70, 120, 25);
        frame.add(labelPay);

        JTextField textPay = new JTextField();
        textPay.setBounds(180, 70, 150, 25);
        frame.add(textPay);

        // 结果显示标签
        JLabel resultLabel = new JLabel("");
        resultLabel.setBounds(50, 200, 300, 25);
        frame.add(resultLabel);

        // 创建按钮
        JButton button = new JButton("计算找零");
        button.setBounds(140, 120, 120, 30);
        frame.add(button);

        // 按钮点击事件
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    double price = Double.parseDouble(textPrice.getText());
                    double pay = Double.parseDouble(textPay.getText());

                    // 验证输入
                    if (price > pay) {
                        resultLabel.setText("❌ 付款金额不足!");
                        return;
                    }
                    if (price > 100 || pay > 100 || price < 0 || pay < 0) {
                        resultLabel.setText("❌ 无效输入,请重新输入!");
                        return;
                    }

                    // 计算找零
                    double value = pay - price;
                    int N50 = 0, N10 = 0, N5 = 0, N1 = 0;

                    if (value / 50 >= 1) {
                        N50 = (int) (value / 50);
                        value = value - 50 * N50;
                    }
                    if (value / 10 >= 1) {
                        N10 = (int) (value / 10);
                        value = value - 10 * N10;
                    }
                    if (value / 5 >= 1) {
                        N5 = (int) (value / 5);
                        value = value - 5 * N5;
                    }
                    N1 = (int) (value);

                    // 显示找零结果
                    resultLabel.setText("<html>找零结果:<br>"
                            + "50元:" + N50 + " 张<br>"
                            + "10元:" + N10 + " 张<br>"
                            + "5元:" + N5 + " 张<br>"
                            + "1元:" + N1 + " 张</html>");
                } catch (NumberFormatException ex) {
                    resultLabel.setText("❌ 请输入有效的数字!");
                }
            }
        });

        // 显示窗口
        frame.setVisible(true);
    }
}

 

posted @ 2025-03-23 09:30  记得关月亮  阅读(7)  评论(0)    收藏  举报