其他分享
首页 > 其他分享> > 趣味数学--贷款计算(图形化界面版)

趣味数学--贷款计算(图形化界面版)

作者:互联网

1、用图形用户界面完成第4单元作业题1:趣味数学题

马克思手稿中有这样一道趣味数学题:男人、女人和小孩总计n个人,在一家饭店里吃饭,共花了cost先令,每个男人各花3先令,每个女人各花2先令,每个小孩各花1先令,请用穷举法编程计算男人、女人和小孩各有几个。

要求:设计图形界面,程序运行后,用户在图形界面中输入人数和总花费。若问题有解,计算男人、女人和小孩的数量并输出;若问题无解,则输出“No answer"。

注意:所有最终结果信息不要输出在控制台。

下面展示一些 内联代码片

JFrame Designer
趣味数学
/*
 * Created by JFormDesigner on Tue May 25 11:54:11 CST 2021
 */

package 慕课Java.第八单元;

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

/**
 * @author 介绍
 */
public class 趣味数学_图形 extends JFrame {

    String columns[] = {"男人","女人","小孩"};
    public 趣味数学_图形() {
        setDefaultCloseOperation(3);
        setTitle("趣味数学");
        initComponents();
    }

    public static void main(String[] args) {
        new 趣味数学_图形();
    }

    //确定按钮监听
    private void button1ActionPerformed(ActionEvent e) {
        // TODO add your code here
        pre();
    }

    //处理
    private void pre(){
        String s1 = textField1.getText();
        String s2 = textField2.getText();
        if(s1.equals("")||s2.equals("")){
            JOptionPane.showMessageDialog(this,"请输入");
        }else{
            int n = Integer.parseInt(s1);
            int c = Integer.parseInt(s2);
            String data[][] = new String[c][c];
            int flag = 0;
            int idx=0;
            for (int i = 0; i <= n; i++) {
                for (int j = 0; j <= n; j++) {
                    for (int k = 0; k <= n; k++) {
                        if (i + j + k == n && 3 * i + 2 * j + k == c) {
                            String s3 = Integer.valueOf(i).toString();
                            String s4 = Integer.valueOf(j).toString();
                            String s5 = Integer.valueOf(k).toString();
                            String s[] = {s3,s4,s5};
                            data[idx] = s;
                            idx++;
                            System.out.println(i + " " + j + " " + k);
                            flag = 1;
                        }
                    }
                }
            }
            if (flag == 0) {
                System.out.println("No answer");
                JOptionPane.showMessageDialog(this,"No answer");
            }else{
                new 显示(data,columns);
            }
        }
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        // Generated using JFormDesigner Evaluation license - 介绍
        label1 = new JLabel();
        label2 = new JLabel();
        textField1 = new JTextField();
        textField2 = new JTextField();
        button1 = new JButton();

        //======== this ========
        setVisible(true);
        var contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- label1 ----
        label1.setText("num");
        label1.setFont(label1.getFont().deriveFont(label1.getFont().getSize() + 5f));
        contentPane.add(label1);
        label1.setBounds(65, 60, 80, 55);

        //---- label2 ----
        label2.setText("cost");
        label2.setFont(label2.getFont().deriveFont(label2.getFont().getSize() + 5f));
        contentPane.add(label2);
        label2.setBounds(65, 165, 80, 55);
        contentPane.add(textField1);
        textField1.setBounds(155, 55, 320, 65);
        contentPane.add(textField2);
        textField2.setBounds(155, 155, 320, 65);

        //---- button1 ----
        button1.setText("\u786e\u5b9a");
        button1.setFont(button1.getFont().deriveFont(button1.getFont().getSize() + 8f));
        button1.setBackground(new Color(153, 204, 255));
        button1.addActionListener(e -> button1ActionPerformed(e));
        contentPane.add(button1);
        button1.setBounds(215, 275, 165, 105);

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(new Dimension(550,400));
        }
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    // Generated using JFormDesigner Evaluation license - 介绍
    private JLabel label1;
    private JLabel label2;
    private JTextField textField1;
    private JTextField textField2;
    private JButton button1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}

显示
/*
 * Created by JFormDesigner on Tue May 25 11:57:19 CST 2021
 */

package 慕课Java.第八单元;

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

/**
 * @author 介绍
 */
public class 显示 extends JFrame {
    String data[][] = null;//内容数据
    String columns[] = null;//题头

    public 显示(String data[][],String columns[]) {
        this.data = data;
        this.columns = columns;
        this.setTitle("显示");
        initComponents();
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        // Generated using JFormDesigner Evaluation license - 介绍
        scrollPane1 = new JScrollPane();
        table1 = new JTable(data,columns);

        //======== this ========
        setVisible(true);
        var contentPane = getContentPane();
        contentPane.setLayout(null);

        //======== scrollPane1 ========
        {
            scrollPane1.setViewportView(table1);
        }
        contentPane.add(scrollPane1);
        scrollPane1.setBounds(10, -5, 525, scrollPane1.getPreferredSize().height);

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    // Generated using JFormDesigner Evaluation license - 介绍
    private JScrollPane scrollPane1;
    private JTable table1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}

2、用图形用户界面完成第4单元作业题2:贷款计算

为了认清校园贷的本质,请根据下面的描述,计算校园贷的实际花费。 用户借贷为等额本息贷款,贷款月利率为0.9%,每月还款金额计算如下式所示,

假设李雷贷款n元,分m月还清,贷款平台需要扣除本金的20%作为保证金,利率按足额贷款进行计算。

要求:设计图形界面,程序运行后,用户在图形界面中输入贷款金额和还款月数,计算李雷实际贷到的金额以及加上本金共需还款金额(结果四舍五入)并输出。

注意:所有最终结果信息不要输出在控制台。

下面展示一些 内联代码片

贷款计算
/*
 * Created by JFormDesigner on Tue May 25 12:27:03 CST 2021
 */

package 慕课Java.第八单元;

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

/**
 * @author 介绍
 */
public class 贷款计算 extends JFrame {
    public 贷款计算() {
        setTitle("贷款计算");
        setDefaultCloseOperation(3);
        initComponents();
    }

    public static void main(String[] args) {
        new 贷款计算();
    }

    //确定按钮监听
    private void button1ActionPerformed(ActionEvent e) {
        // TODO add your code here
        pre();
    }

    //处理
    private void pre(){
        String s1 = textField1.getText();
        String s2 = textField2.getText();
        if(s1.equals("")||s2.equals("")){
            JOptionPane.showMessageDialog(this,"请输入");
        }else{
            int sum=0;
            int n = Integer.parseInt(s1);
            int m = Integer.parseInt(s2);
            System.out.println(n/10*8);
            double temp = 1.009;
            for(int i=1;i<=m;i++){
                sum+=(n*((Math.pow(temp,m)*0.009)/(Math.pow(temp,m)-1)));
            }
            System.out.println(Math.round(sum+n));
            JOptionPane.showMessageDialog(this,""+Math.round(sum+n));
        }
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        // Generated using JFormDesigner Evaluation license - 介绍
        label1 = new JLabel();
        label2 = new JLabel();
        textField1 = new JTextField();
        textField2 = new JTextField();
        button1 = new JButton();

        //======== this ========
        setVisible(true);
        var contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- label1 ----
        label1.setText("\u8d37\u6b3e\u91d1\u989d");
        label1.setFont(label1.getFont().deriveFont(label1.getFont().getSize() + 5f));
        contentPane.add(label1);
        label1.setBounds(65, 60, 80, 55);

        //---- label2 ----
        label2.setText("\u8fd8\u6b3e\u6708\u6570");
        label2.setFont(label2.getFont().deriveFont(label2.getFont().getSize() + 5f));
        contentPane.add(label2);
        label2.setBounds(65, 165, 80, 55);
        contentPane.add(textField1);
        textField1.setBounds(155, 55, 320, 65);
        contentPane.add(textField2);
        textField2.setBounds(155, 155, 320, 65);

        //---- button1 ----
        button1.setText("\u786e\u5b9a");
        button1.setFont(button1.getFont().deriveFont(button1.getFont().getSize() + 8f));
        button1.setBackground(new Color(153, 204, 255));
        button1.addActionListener(e -> button1ActionPerformed(e));
        contentPane.add(button1);
        button1.setBounds(215, 275, 165, 105);

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(new Dimension(550,400));
        }
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    // Generated using JFormDesigner Evaluation license - 介绍
    private JLabel label1;
    private JLabel label2;
    private JTextField textField1;
    private JTextField textField2;
    private JButton button1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}

标签:--,preferredSize,button1,JFormDesigner,contentPane,private,趣味,new,图形化
来源: https://blog.csdn.net/NOfighting/article/details/117253568