Java小练——课程均分计算器

1、功能

可从该计算器界面中输入课程名称、课程学分、课程成绩,每输入一门课程点击“添加”按钮可自动创建文本文档并保存至该文本文档。输入完毕后点击计算按钮即可计算出总学分以及加权平均分。
界面展示

2、实现方式

1、java图形化界面设计
2、文件输入输出流

3、代码

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.Collections;
import java.util.Vector;

public class MarkManage {
    public static void main(String args[]){
        try {
            new ManageFrame();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    static class ManageFrame extends JFrame {
        private JButton btn1;    //保存学生信息按钮
        private JButton btn3;    //计算按钮
        private JTextField courseField;
        private JTextField creditField;
        private JTextField markField;
        private JTextField sumCreditField;
        private JTextField averageField;

        public ManageFrame() throws Exception{
            super("课程均分计算器");
            init();    //放入组件
            setBounds(200,200,500,120);
            this.getContentPane().setBackground(Color.pink);
            
            File StudentFile = new File("D:\\Course.txt");
            FileOutputStream fos1 = new FileOutputStream(StudentFile,true);
            FileInputStream fis = new FileInputStream(StudentFile);
            
            btn1.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    try{
                        String a = courseField.getText()+"   学分"+creditField.getText()+"   成绩"+markField.getText()+"\n";
                        byte[] b = new byte[1024];
                        b = a.getBytes();
                        fos1.write(b,0,b.length);
                        System.out.println("成绩录入成功!");
                    }catch (IOException e1){
                        System.out.println(e1);
                    }
                    courseField.setText("");
                    creditField.setText("");
                    markField.setText("");
                }
            });

            btn3.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    BufferedReader bf  = new BufferedReader(new InputStreamReader(fis));
                    String s;
                    Vector<Double>MarkArray1 = new Vector<>(0);
                    Vector<Double>MarkArray2 = new Vector<>(0);
                    Double sum1 = 0.0;
                    Double sum2 = 0.0;
                    try{
                        while ((s=bf.readLine()) != null){
                            String p = s.substring(s.indexOf('绩')+1);
                            int x = s.indexOf('分');
                            String q = s.substring(x+1,x+5);
                            MarkArray1.add(Double.parseDouble(q));
                            MarkArray2.add(Double.parseDouble(p)*Double.parseDouble(q));
                        }
                        Collections.sort(MarkArray1);
                        for(int i = 0;i<MarkArray1.size();i++){
                            sum1 += MarkArray1.get(i);
                        }
                        for(int j = 0;j<MarkArray2.size();j++){
                            sum2 += MarkArray2.get(j);
                        }
                        sumCreditField.setText(String.valueOf(sum1));
                        averageField.setText(String.valueOf(sum2/sum1));
                        String m = "总学分:"+sum1+"      "+"均分:"+averageField.getText();
                        byte [] bt = new byte[1024];
                        bt = m.getBytes();
                        fos1.write(bt,0,bt.length);
                        System.out.println("成绩计算成功!");
                    }catch (IOException e1){

                    }
                }
            });
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setVisible(true);
        }
        //窗口设置
        public void init(){
            Container container = this.getContentPane();
            btn1 = new JButton("添加");
            btn3 = new JButton("计算");
            courseField = new JTextField(9);
            creditField = new JTextField(9);
            markField = new JTextField(9);
            averageField = new JTextField(12);
            sumCreditField = new JTextField(8);
            JLabel jLabel1 = new JLabel("课程名称:");
            JLabel jLabel2 = new JLabel("学分:");
            JLabel jLabel3 = new JLabel("成绩:");
            JLabel jLabel4 = new JLabel("均分:");
            JLabel jLabel5 = new JLabel("总学分:");
            JPanel jPanel1 = new JPanel();
            JPanel jPanel2 = new JPanel();

            jPanel1.setBackground(Color.pink);
            jPanel2.setBackground(Color.pink);
            container.add(jPanel1);
            container.add(jPanel2);
            jPanel1.add(jLabel1);
            jPanel1.add(courseField);
            jPanel1.add(jLabel2);
            jPanel1.add(creditField);
            jPanel1.add(jLabel3);
            jPanel1.add(markField);
            jPanel2.add(btn1);
            jPanel2.add(btn3);
            jPanel2.add(jLabel5);
            jPanel2.add(sumCreditField);
            jPanel2.add(jLabel4);
            jPanel2.add(averageField);
            
            setLayout(new FlowLayout(FlowLayout.CENTER));
        }
    }
}

4、演示

添加成绩
计算结果
自动保存的文本文档

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值