文章出處

 

開發人員:鄭勝斌   開發時間: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學好。在此還要感謝我的合作伙伴,幫我解決了很多我不懂的問題,相信我們以后的合作會越來越好,在此附上我的合照

 

德穎(左)       勝斌(右)

 


文章列表




Avast logo

Avast 防毒軟體已檢查此封電子郵件的病毒。
www.avast.com


arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()