zoukankan      html  css  js  c++  java
  • 作业2 四则运算

    四则运算

    编译环境:Eclipse

    开发人员:钟鹏升,吴梓健

    开发时间:2015-04-07到2015-04-09

    实现功能:

    1. 登陆界面,用户在第一次答题时,需要用户输入用户名,用户下次启动后,程序记住用户前一次输入的用户名
    2. 程序可以出0到100的正整数四则运算(不会出现除数为0的情况),除法保留两位小数且四舍五入,如:26/3= 8.67,特别注意:这里是8.67而非8.66
    3. 用户可以选择出题的个数(最多不能超过5个题目),题目做错会在下一题显示正确答案,在题目最后显示测验的题目,答对的题目,正确率。
    4. 程序可以设置皮肤功能,可以改变界面的颜色即可。
    5. 程序具有提示输入错误功能,自动判别输入的内容,如果输入错误会提示输入错误

    缺点:

          1.界面还不够美观

        2.还没弄结果可以为分数形式,没弄表达式里含有负整数,时间线程还没加入。

    结伴同学:

    姓名:吴梓健

    博客地址:http://www.cnblogs.com/wuzijian/

    总结:

        1.很久没有用JAVA了,部分的知识有点忘记,遇到很多问题,经过百度和查看书籍后解决

        2.可以参考其它同学的代码,从中可以学到别人的想法和代码格式

        3.写代码一定要规范,最好有一个自己写的模板,多点练习,尝试做一些小型程序

    下图是运行的结果截图(共3张):

    工作时的照片:

    下面是源代码:

    import javax.swing.*;
    import javax.swing.table.*;
    
    import java.awt.*;
    import java.awt.event.*;
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.*;
    
    public class MainFrame extends JFrame implements ActionListener{
        JLabel label1 = new JLabel("");
        JLabel label2 = new JLabel("");
        JLabel label3 = new JLabel("用户:");
        JTextField textField = new JTextField(10);
        JButton DengluBtn = new JButton("登陆");
        public Font firstFont = new Font("宋体",Font.PLAIN,20);
        public Font secondFont = new Font("黑体",Font.PLAIN,22);
        String yonghu;
        public MainFrame()
        {
            try{
                this.setDefaultCloseOperation(EXIT_ON_CLOSE);
                jbInit();
            }catch(Exception e){
                e.printStackTrace();
            }
        }
        private void jbInit() throws Exception{
            setTitle("登陆");
            this.setDefaultCloseOperation(EXIT_ON_CLOSE);
            setSize(400,400);
            this.setLayout(null);
            label1.setFont(firstFont);
            label2.setFont(firstFont);
            label3.setFont(secondFont);
            label1.setBounds(50, 80, 300, 50);
            label2.setBounds(50, 80, 300, 50);
            label3.setBounds(80, 160, 80, 50);
            textField.setBounds(150, 170, 100, 30);
            DengluBtn.setBounds(260, 165, 60, 40);
            DengluBtn.addActionListener(this);
            yonghu = textField.getText();
            try{            
                File fis = new File("foo.txt"); 
                BufferedReader bis=new BufferedReader(new FileReader(fis));
                String temp="";
                temp=bis.readLine();
                if(temp==null)
                {
                    label1.setVisible(false);
                    label2.setText("还没有用户,请随意输入以登陆!");
                }
                bis.close();
                label1.setText("之前的用户为:"+temp);
            }catch(FileNotFoundException ex){
                label1.setVisible(false);
                label2.setText("还没有用户,请随意输入以登陆!");
            }
             catch (IOException ex) {
                // TODO Auto-generated catch block
                ex.printStackTrace();
            }
            this.add(label1);
            this.add(label2);
            this.add(label3);
            this.add(textField);
            this.add(DengluBtn);
        }
        public void actionPerformed(ActionEvent e)
        {
            if(e.getSource()==DengluBtn)
            {
                try{ 
                    File fs = new File("foo.txt"); 
                    BufferedWriter os = new BufferedWriter(new FileWriter(fs));
                    os.write(textField.getText());
                    os.flush();
                    os.close(); 
                }catch(Exception ex){ 
                    ex.printStackTrace(); 
                    }     
                Cal cal=new Cal();
                this.setVisible(false);
                Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
                Dimension frameSize=cal.getSize();
                cal.setLocation((screenSize.width-frameSize.width)/2, (screenSize.height-frameSize.height)/2);
                cal.setVisible(true);
            }
        }
    }
    import javax.swing.*;
    import javax.swing.table.*;
    
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    public class Cal extends JFrame implements ActionListener {
        public static String number;
         JLabel label = new JLabel("题目的数量:");
         JTextField textField = new JTextField(10);
         JButton confirmBtn = new JButton("确定");
         public Font firstFont = new Font("宋体",Font.PLAIN,24);
         public Cal()
            {
                try{
                    jbInit();
                }catch(Exception e){
                    e.printStackTrace();
                }
            }
            private void jbInit() throws Exception{
                setTitle("四则运算测试");
                setSize(475,450);
                this.setLayout(null);
                label.setBounds(60, 180, 150, 40);
                label.setFont(firstFont);
                confirmBtn.setBounds(310, 175, 60, 50);
                textField.setBounds(200, 180, 100, 40);
                confirmBtn.addActionListener(this);
                this.add(label);
                this.add(textField);
                this.add(confirmBtn);
            }
            public void actionPerformed(ActionEvent e)
            {
                if(e.getSource()==confirmBtn)
                {
                    number = textField.getText();            
                    
                    if(!checkString(number)){
                            JOptionPane.showMessageDialog(null, "未输入数字!","提示",JOptionPane.ERROR_MESSAGE);
                            return;
                    }
                    else if((!checkInteger(number))){
                            textField.setText("");
                            JOptionPane.showMessageDialog(null, "请输入数字!","提示",JOptionPane.ERROR_MESSAGE);
                            return;
                    }
                    else if(Integer.parseInt(number)<=0){
                        textField.setText("");
                        JOptionPane.showMessageDialog(null, "输入错误!","提示",JOptionPane.ERROR_MESSAGE);
                        return;
                }
                    else if(Integer.parseInt(number)>5){
                        textField.setText("");
                        JOptionPane.showMessageDialog(null, "5道题以内!","提示",JOptionPane.ERROR_MESSAGE);
                        return;
                }
                    else {
                        CountPanel count=new CountPanel();
                        this.remove(this.getContentPane());
                        this.setContentPane(count);
                        this.setVisible(true);
                    }                                                                
                }
            }
            /**
             * 检查输入的文本信息是否为空
             * @param text 输入的信息内容
             * @return 输入的文本信息是否为空
             */
            public boolean checkString(String text) {
                    /*      当输入的文本去掉前后空格长度为0时返回false  */
                    if(text.trim().length()==0){
                            return false;
                    }
                    return true;
            }
            
            /**
             * 检查输入的数字信息是否是整数
             * @param text 输入的信息内容
             * @return 输入的数字信息是否是整数
             */
            public boolean checkInteger(String text) {
                    /*      当输入的文本去掉前后空格长度为0时返回false  */
                    if(text.trim().length()==0){
                            return false;
                    }
                    try{
                            Integer.parseInt(text);
                    }catch(Exception e){
                            /*      无法转换为整数时返回false   */
                            return false;
                    }
                    return true;
            }
    }
    import java.awt.*;
    import java.awt.event.*;
    import java.math.*;
    import java.text.DecimalFormat;
    import java.util.Random;
    
    import javax.swing.*;
    
    public class CountPanel extends JPanel implements ActionListener {
        JLabel L=new JLabel("");
        JLabel L1=new JLabel("");
        JLabel L2=new JLabel("");
        JLabel L3=new JLabel("");
        JLabel L4=new JLabel("");
        JTextField T = new JTextField(10);
        JButton B = new JButton("提交");
        JButton colorBtn=new JButton("变色");
        JPanel P1=new JPanel();
        public Font firstFont = new Font("宋体",Font.PLAIN,22);
        public Font secondFont = new Font("幼圆",Font.PLAIN,20);
        public Font thirdFont = new Font("幼圆",Font.PLAIN,18);
        int r=0;
        int n=1;
        int op;
        int a;
        int b;
        double ra;
        String t;
        Double answer;
        public CountPanel()
        {
            try{
                jbInit();
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
        private void jbInit() throws Exception
        {
            this.setLayout(null);
            L.setBounds(60, 150, 150, 40);
            L.setFont(firstFont);
            T.setBounds(190, 150, 80, 40);
            B.setBounds(280, 145, 60, 50);
            colorBtn.setBounds(380, 20, 60, 50);
            L1.setBounds(40, 220, 400, 40);       
            L1.setFont(secondFont);
            L2.setFont(thirdFont);
            L3.setFont(thirdFont);
            L4.setFont(thirdFont);
            P1.setBounds(120, 300, 200, 200);
            B.addActionListener(this);
            colorBtn.addActionListener(new ActionListener() {
                
                @Override
                public void actionPerformed(ActionEvent e) {
                    setBackground(new Color(
                              (new Double(Math.random() * 128)).intValue() + 128,   
                              (new Double(Math.random() * 128)).intValue() + 128,   
                              (new Double(Math.random() * 128)).intValue() + 128));
                    P1.setBackground(new Color(
                              (new Double(Math.random() * 128)).intValue() + 128,   
                              (new Double(Math.random() * 128)).intValue() + 128,   
                              (new Double(Math.random() * 128)).intValue() + 128));
                    
                }
            });
            a =new Random().nextInt(100);
            b =new Random().nextInt(100);
            op=new Random().nextInt(4);
            switch(op)
            {
            case 0:
                L.setText("题"+n+". "+a+"+"+b+"=");
                answer=(double) (a+b);
                break;
            case 1:
                L.setText("题"+n+". "+a+"-"+b+"=");
                answer=(double) (a-b);
                break;
            case 2:
                L.setText("题"+n+". "+a+"*"+b+"=");
                answer=(double) (a*b);
                break;
            case 3:
                while(b==0){
                    b =new Random().nextInt(100);
                }
                L.setText("题"+n+". "+a+"/"+b+"=");
                BigDecimal   x   =   new   BigDecimal((double)a/b);  
                answer  =   x.setScale(2,   BigDecimal.ROUND_HALF_UP).doubleValue();  
                
                break;
            }
            this.add(L);
            this.add(T);
            this.add(B);
            this.add(L1);
            this.add(P1);
            this.add(colorBtn);
            P1.add(L2);
            P1.add(L3);
            P1.add(L4);
        }
        
        public void actionPerformed(ActionEvent e)
        {
            if(e.getSource()==B)
            {
                t = T.getText();            
                
                /*      检查学号是否输入      */
                if(!checkString(t)){
                        JOptionPane.showMessageDialog(null, "未输入数字!","提示",JOptionPane.ERROR_MESSAGE);
                        return;
                }
                else if((!checkInteger(t))){
                        T.setText("");
                        JOptionPane.showMessageDialog(null, "请输入数字!","提示",JOptionPane.ERROR_MESSAGE);
                        return;
                }
                else {
                    n++;
                    if(Double.parseDouble(t)==answer){
                        r++;
                        L1.setText("               答对了!");
                    }
                    else{
                        L1.setText("答错了!"+L.getText()+"的正确答案是:"+answer);
                    }
                    T.setText("");
                    if(n<=Integer.parseInt(Cal.number)){
                        a =new Random().nextInt(100);
                        b =new Random().nextInt(100);
                        op=new Random().nextInt(4);
                        System.out.print(op);
                        switch(op)
                        {
                        case 0:
                            L.setText("题"+n+". "+a+"+"+b+"=");
                            answer=(double) (a+b);
                            break;
                        case 1:
                            L.setText("题"+n+". "+a+"-"+b+"=");
                            answer=(double) (a-b);
                            break;
                        case 2:
                            L.setText("题"+n+". "+a+"*"+b+"=");
                            answer=(double) (a*b);
                            break;
                        case 3:
                            while(b==0){
                                b =new Random().nextInt(100);
                            }
                            L.setText("题"+n+". "+a+"/"+b+"=");
                            BigDecimal   x   =   new   BigDecimal((double)a/b);  
                             answer  =   x.setScale(2,   BigDecimal.ROUND_HALF_UP).doubleValue();  
                            break;
                        }
                    }
                    else
                        {
                            B.setEnabled(false);
                            L2.setText("本次测验共"+Integer.parseInt(Cal.number)+"题");
                            L3.setText("你答对了"+r+"题");
                            ra=(double)r/(n-1);
                            DecimalFormat fmt = new DecimalFormat("0.##%");
                            L4.setText("正确率为:"+fmt.format(ra));
                        }
                }  
            }
        }
        /**
         * 检查输入的文本信息是否为空
         * @param text 输入的信息内容
         * @return 输入的文本信息是否为空
         */
        public boolean checkString(String text) {
                /*      当输入的文本去掉前后空格长度为0时返回false  */
                if(text.trim().length()==0){
                        return false;
                }
                return true;
        }
        
        /**
         * 检查输入的数字信息是否是整数
         * @param text 输入的信息内容
         * @return 输入的数字信息是否是整数
         */
        public boolean checkInteger(String text) {
                /*      当输入的文本去掉前后空格长度为0时返回false  */
                if(text.trim().length()==0){
                        return false;
                }
                try{
                        Double.parseDouble(text);
                }catch(Exception e){
                        /*      无法转换为整数时返回false   */
                        return false;
                }
                return true;
        }
    }
    import java.awt.*;
    import javax.swing.*;
    
    public class Test {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            MainFrame  frame=new MainFrame();
            Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
            Dimension frameSize=frame.getSize();
            frame.setLocation((screenSize.width-frameSize.width)/2, (screenSize.height-frameSize.height)/2);
            frame.setVisible(true);
        }
    }
  • 相关阅读:
    整理一下反射
    数据查询--整理(更新中)
    【Django】Django model与数据库操作对应关系(转)
    给定 2 个正整数 a, b,a 和 b 最多可能有 40 位,求出 a + b 的和(C语言实现)
    c++和QT实现俄罗斯方块,使用GraphicsView。
    C语言的动态函数库和静态函数库的生成和使用(linux环境下)
    linux内核驱动的知识要点
    简单的linux内核移植知识
    快速排序c++/c实现
    c语言系统函数——进程的创建
  • 原文地址:https://www.cnblogs.com/SshengS/p/4410390.html
Copyright © 2011-2022 走看看