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

    开发人员:郑胜斌   开发时间:2015-04-07

    开发环境:eclipse

    提交次数:N+1次

    实现功能:

        1.基本的加减乘除

        2.每次出1道题目,提交后会显示是否答对,如果错了,还会出现正确答案

           3.题目是随机的

        4.能出于分数相关的运算以及可以输入真分数

           5.可以控制题目的数量

    缺点:

        1.分数计算的答案输入分数格式才显示正确

        2.不能用户选择做什么运算

          3.还不能统计答题的时间

          4.不能统计答题正确的数目

    个人体会:

        1.能力还是不足,一些东西想到了却不能实现

        2.要多点和伙伴讨论

        3.对于Java的基础不够熟

    分工合作:

               孔:1.算法的界面

                     2.代码的整合

                     3.核心代码的编译

              郑:1.基本算法的编译

                    2.寻找算法的资料

                    3.文档的整理

    合作伙伴 孔德颖 http://www.cnblogs.com/kong21/

    import java.io.*;

    import java.math.RoundingMode;

    import java.text.DecimalFormat;

    import java.util.*;

    import java.util.regex.Pattern;

    import java.awt.*;

    import java.awt.event.*;

    import javax.swing.*;

    import java.math.*;

    public class Rj11 {

     

        public static void main(String[] args) {

            MyFrame frame=new MyFrame();

     

        }

    }

    class MyFrame extends JFrame{

        private JLabel L1=new JLabel("题目数");

        private JLabel L2=new JLabel("题目是");

        private JLabel L3=new JLabel("答案是");

        JTextField T1 = new JTextField(15);

        JTextField T2 = new JTextField(15);

        JTextField T3 = new JTextField(5);

        JTextField T4 = new JTextField(20);

        JButton B4=new JButton("确定");

        JButton B1=new JButton("开始");

        JButton B2=new JButton("提交");

        JButton B3=new JButton("下一题");

        MyFrame(){

            this.setTitle("四则运算");

            this.setSize(400, 250);

            this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

            this.getContentPane().setBackground(Color.white);

            FlowLayout flow=new FlowLayout(FlowLayout.LEFT,10,15);

            this.setLayout(flow);

            B4.addActionListener(new ActionListener(){

                public void actionPerformed(ActionEvent e){

                    Pattern pa = Pattern.compile("[0-9]*");

                    String s1=T1.getText();

                    if(pa.matcher(s1).matches())//检测用户输入是否正确

                    {

                        try

                        {

                            int number=Integer.parseInt(s1);

                            File dir=new File("d:number");

                            if(!dir.exists())

                            {

                                dir.mkdir();

                            }

                            File afile=new File(dir,"data.txt");

                            afile.createNewFile();

                            BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(afile)));

                            bw.write(String.valueOf(s1));

                            bw.close();

                        }

                        catch(IOException t)

                        {

                            T4.setText("IO exception thrown:"+t);

                        }

                    }

                    else

                    {

                        T1.setText("输入的格式错误请重新输入!");

                    }

                }

            });

            B1.addActionListener(new ActionListener(){

                public void actionPerformed(ActionEvent y){

                    DecimalFormat df=new DecimalFormat("#.00");

                    df.setRoundingMode(RoundingMode.HALF_UP);

                    char[]ch={'+','-','*','/'};

                    int c = 0;

                    int d=0;

                    int h=0;

                    int f=0;

                    int e=0;

                    double answer;

                    double ans;

                    int right=0;

                    int number;

                    try

                    {

                        File dir1=new File("d:number");

                        File afile1=new File(dir1,"data.txt");

                        BufferedReader br1=new BufferedReader(new InputStreamReader(new FileInputStream(afile1)));

                        String strRead1=br1.readLine();

                        number=Integer.parseInt(strRead1);

                        if(number>=1)

                        {

                            int n = new Random().nextInt(5);//获取运算的方式0—— +    1—— -    2—— *    3—— /

                            if(n==0 || n==2 || n==3 || n==1)//int型的运算

                            {

     

                                double a1 = new Random().nextDouble()*100;//获取式子的运算随机数

                                double a=Double.parseDouble(df.format(a1));

                                double b1 =new Random().nextDouble()*100+1;//除数不能为0

                                double b=Double.parseDouble(df.format(b1));

                                T2.setText(a+""+ch[n]+""+b+"=");//输出式子

     

                                if(n==0)//加法

                                {   

                                    answer=a+b;

                                    answer=Double.parseDouble(df.format(answer));//保留两位小数

                                    String dirName1="d:MyData";

                                    try

                                    {

                                        File dir=new File(dirName1);

                                        if(!dir.exists())

                                        {

                                            dir.mkdir();

                                        }

                                        File afile=new File(dir,"data.txt");

                                        afile.createNewFile();

                                        BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(afile)));

                                        bw.write(String.valueOf(answer));

                                        bw.close();

                                    }

                                    catch(IOException t)

                                    {

                                        T4.setText("IO exception thrown:"+t);

                                    }

                                }

                                else if(n==1)//减法

                                {   

                                    answer=a-b;

                                    answer=Double.parseDouble(df.format(answer));//保留两位小数

                                    String dirName1="d:MyData";

                                    try

                                    {

                                        File dir=new File(dirName1);

                                        if(!dir.exists())

                                        {

                                            dir.mkdir();

                                        }

                                        File afile=new File(dir,"data.txt");

                                        afile.createNewFile();

                                        BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(afile)));

                                        bw.write(String.valueOf(answer));

                                        bw.close();

                                    }

                                    catch(IOException t)

                                    {

                                        T4.setText("IO exception thrown:"+t);

                                    }

                                }

                                else if(n==2) //乘法

                                {   

                                    answer=a*b;

                                    answer=Double.parseDouble(df.format(answer));//保留两位小数

                                    String dirName1="d:MyData";

                                    try

                                    {

                                        File dir=new File(dirName1);

                                        if(!dir.exists())

                                        {

                                            dir.mkdir();

                                        }

                                        File afile=new File(dir,"data.txt");

                                        afile.createNewFile();

                                        BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(afile)));

                                        bw.write(String.valueOf(answer));

                                        bw.close();

                                    }

                                    catch(IOException t)

                                    {

                                        T4.setText("IO exception thrown:"+t);

                                    }

                                }

                                else if(n==3)  //除法

                                {   

     

                                    ans=Double.parseDouble(df.format(a/b));//保留两位小数

                                    String dirName1="d:MyData";

                                    try

                                    {

                                        File dir=new File(dirName1);

                                        if(!dir.exists())

                                        {

                                            dir.mkdir();

                                        }

                                        File afile=new File(dir,"data.txt");

                                        afile.createNewFile();

                                        BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(afile)));

                                        bw.write(String.valueOf(ans));

                                        bw.close();

                                    }

                                    catch(IOException t)

                                    {

                                        T4.setText("IO exception thrown:"+t);

                                    }

                                }

                            }

                            else if(n==4)//含有真分数的运算

                            {

                                double fm=0;

                                double fz=0;

                                double MaxD;

                                String str1;//最简分式

                                boolean t = true;

                                while(t==true)//判断并输出真分数

                                {

                                    c = new Random().nextInt(10);

                                    d =new Random().nextInt(10);//获取式子的运算随机数

                                    if(c<d)

                                        t=false;

                                }

                                t=true;

                                while(t==true)//判断并输出真分数

                                {

                                    e =new Random().nextInt(10)+1;

                                    f =new Random().nextInt(10);//获取式子的运算随机数

                                    if(e<f)

                                        t=false;

     

                                }

                                n=new Random().nextInt(4);

                                T2.setText("("+c+"/"+d+")"+ch[n]+"("+e+"/"+f+")"+"=");

                                if(n==0)//加法

                                { 

                                    fm=d*f;

                                    fz=(c*f)+(e*d);

                                    //简化约分

                                    MaxD=getMaxDivi(fz,fm);//最大公约数

                                    fz=fz/MaxD;

                                    fm=fm/MaxD;

     

                                    //double不保留小数

                                    //简化完毕

                                    //标准答案

                                    str1=(int)fz+"/"+(int)fm;//得出最简答案

                                    ans=Double.parseDouble(df.format(fz/fm));

                                    String dirName1="d:MyData";

                                    try

                                    {

                                        File dir=new File(dirName1);

                                        if(!dir.exists())

                                        {

                                            dir.mkdir();

                                        }

                                        File afile=new File(dir,"data.txt");

                                        afile.createNewFile();

                                        BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(afile)));

                                        bw.write(str1);

                                        bw.close();

                                    }

                                    catch(IOException t1)

                                    {

                                        T4.setText("IO exception thrown:"+t);

                                    }

                                }

                                else if(n==1)//减法

                                {   

                                    fm=d*f;

                                    fz=(c*f)-(e*d);

                                    MaxD=getMaxDivi(fz,fm);

                                    fz=fz/MaxD;

                                    fm=fm/MaxD;

                                    if(fz<0||fm<0)

                                    {

                                        fz=-(Math.abs(fz));

                                        fm=Math.abs(fm);

     

                                    }

                                    str1=(int)fz+"/"+(int)fm;//正确答案的String

                                    String dirName1="d:MyData";

                                    try

                                    {

                                        File dir=new File(dirName1);

                                        if(!dir.exists())

                                        {

                                            dir.mkdir();

                                        }

                                        File afile=new File(dir,"data.txt");

                                        afile.createNewFile();

                                        BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(afile)));

                                        bw.write(str1);

                                        bw.close();

                                    }

                                    catch(IOException t1)

                                    {

                                        T4.setText("IO exception thrown:"+t);

                                    }

                                }

                                else if(n==2)//乘法

                                {   

                                    fz=c*e;

                                    fm=d*f;

                                    MaxD=getMaxDivi(fz,fm);

                                    fz=fz/MaxD;

                                    fm=fm/MaxD;

     

                                    str1=(int)fz+"/"+(int)fm;//正确答案的String

                                    String dirName1="d:MyData";

                                    try

                                    {

                                        File dir=new File(dirName1);

                                        if(!dir.exists())

                                        {

                                            dir.mkdir();

                                        }

                                        File afile=new File(dir,"data.txt");

                                        afile.createNewFile();

                                        BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(afile)));

                                        bw.write(str1);

                                        bw.close();

                                    }

                                    catch(IOException t1)

                                    {

                                        T4.setText("IO exception thrown:"+t);

                                    }

                                }

                                else if(n==3)//除法

                                {   

                                    fz=c*f;

                                    fm=d*e;

                                    MaxD=getMaxDivi(fz,fm);

                                    fz=fz/MaxD;

                                    fm=fm/MaxD;

     

                                    str1=(int)fz+"/"+(int)fm;//正确答案的String

                                    String dirName1="d:MyData";

                                    try

                                    {

                                        File dir=new File(dirName1);

                                        if(!dir.exists())

                                        {

                                            dir.mkdir();

                                        }

                                        File afile=new File(dir,"data.txt");

                                        afile.createNewFile();

                                        BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(afile)));

                                        bw.write(str1);

                                        bw.close();

                                    }

                                    catch(IOException t1)

                                    {

                                        T4.setText("IO exception thrown:"+t);

                                    }

                                }

                            }

                            number--;

                            String ss=String.valueOf(number);

                            File dir=new File("d:number");

                            File afile=new File(dir,"data.txt");

                            BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(afile)));

                            bw.write(String.valueOf(ss));

                            bw.close();

                        }

                        else

                        {

                            T4.setText("");

                        }

                    }

                    catch(IOException t)

                    {

                        T4.setText("IO exception thrown:"+t);

                    }           

                }

            });

            B2.addActionListener(new ActionListener(){

                public void actionPerformed(ActionEvent e){

                    int n;

                    int n1;

                    String dirName="d:MyData";

                    String filename="data.txt";

                    String filename1="data1.txt";

                    try{

                        File dir=new File(dirName);

                        File afile=new File(dir,filename);

                        BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(afile)));

                        String strRead=br.readLine();

                        if(strRead.equals(T3.getText()))

                        {

                           

                            T4.setText("恭喜,你答对了!");

                        }

                        else

                        {

                           

                            T4.setText("对不起,你答错了!正确答案为:"+strRead);

                        }

     

                    }

                    catch(IOException t)

                    {

                        T4.setText("IO exception thrown:"+t);

                    }

     

                }

     

            });

            add(L1);add(T1);add(B4);add(B1);add(L2);add(T2);add(T3);add(B2);add(L3);add(T4);

            this.setResizable(false);

            this.setVisible(true);

        }

        static double getMaxDivi(double a,double b)//求最大公约数,运用辗转相除法

        {

            double maxdivd=0;

            if(a==0||b==0)

            {

                maxdivd=1;

                return maxdivd;

            }

            else

            {

                if(a<b)

                    getMaxDivi(b,a);

                boolean value=false;

                if(a==0)

                {

                    maxdivd=1;

                    value=true;

                }

     

                while(value==false)

                {

                    int n=(int) (a/b);

                    int m=(int) (a%b);

                    if(m==0)

                    {

                        maxdivd=b;

                        value=true;

                    }

                    else

                    {

                        a=b;

                        b=m;

                    }

                }

            }   

            return maxdivd;

        }

    }

    测试图片:

     

    总结:在通过将近一个星期的作业中,使我学到不少的东西,也让我看到了我自己的一些在知识上不足,也将使我在后面对软件工程的学习中还要更加努力,以便在以后能找到自己适意的工作。在这次作业中让我学到了很多的东西,不只是代码上的知识,也有团队上的知识。一个人能力是有限的,团结起来才力量大。同时合理的分工也很重要,及时与伙伴沟通交换资料能有效避免一些不必要的错误。虽然这次作业没有与预期那样好,但我门会继续努力学习,把java学好。在此还要感谢我的合作伙伴,帮我解决了很多我不懂的问题,相信我们以后的合作会越来越好,在此附上我的合照

     

    德颖(左)       胜斌(右)

  • 相关阅读:
    keras系列︱迁移学习:利用InceptionV3进行fine-tuning及预测、完美案例(五)
    keras系列︱人脸表情分类与识别:opencv人脸检测+Keras情绪分类(四)
    keras系列︱图像多分类训练与利用bottleneck features进行微调(三)
    keras系列︱Application中五款已训练模型、VGG16框架(Sequential式、Model式)解读(二)
    将C++资源文件读取出来
    windows驱动程序中的预处理含义
    win10网上邻居看不到别的共享电脑怎么样办
    #pragma alloc_text 与 ALLOC_PRAGMA
    IoAllocateMdl,MmProbeAndLockPages的用法
    Composer三步曲:安装、使用、发布
  • 原文地址:https://www.cnblogs.com/zsb1/p/4411522.html
Copyright © 2011-2022 走看看