博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
5.2 5.3 实验五 四则运算
阅读量:6574 次
发布时间:2019-06-24

本文共 21454 字,大约阅读时间需要 71 分钟。

 

5.2 5.3 实验五 四则运算

 

一、过程简介:

 

工作人员:邵家文       学号:201306114322

 

        李泳江       学号:201306114314          博客:http://www.cnblogs.com/mch-29-wamc/

 

 分工:邵家文:做接口,编译异常

 

      李泳江:记录数据,分析错误的原因。

 

实验环境:Eclipse集成开发环境+java

 

总结:再次跟泳江合作我感到很开心,泳江这次也帮了不少忙,解决了不少问题!

package ShiYanwu2;import java.text.DecimalFormat;import java.util.regex.Matcher;import java.util.regex.Pattern;        class Core {        String[] string=new String[2];        private Pattern pattern= Pattern.compile("[0-9]*");        private Matcher isNum;        private Matcher isNum1;            public    String[] two_number() throws YunSuanFu ,GeShiCuoWu        {                ComeTrueXml text=new ComeTrueXml();                 string= text.getXml();                     isNum = pattern.matcher(string[0]);                 if( isNum.matches())                 {                    int y=Integer.parseInt(string[1]);                    if(y<0)                    {                    throw new YunSuanFu("你设定的运算低于系统设定范围:"+",超出系统的范围!");//抛出异常                      }                                        else if(y>1000)                    {                    throw new YunSuanFu("你设定的运算数范围:"+",超出系统的范围!");//抛出异常                      }                }                else                {                    throw new GeShiCuoWu("你输入设定的数的格式不正确,请重启然后再设定!");//抛出异常                  }             int shu=(int)(Math.random()*3+1);            int shu1=(int)(Math.random()*Integer.parseInt(string[1]));            int shu2=(int)(Math.random()*Integer.parseInt(string[1]));            int shu6;                        String daan;            String daan1;            String gongsi_daan_daan1[]=new String[3];                                    switch(shu)            {            case 1: shu6=shu1+shu2;daan=String.valueOf(shu6);daan=String.valueOf(shu6);            daan=String.valueOf(shu6);daan1=daan;            gongsi_daan_daan1[0]=shu1+"+"+shu2+"=";  gongsi_daan_daan1[1]=daan;  gongsi_daan_daan1[2]=daan1;  break;            case 2: shu6=shu1*shu2;daan=String.valueOf(shu6);daan=String.valueOf(shu6);            daan=String.valueOf(shu6);daan1=daan;            gongsi_daan_daan1[0]=shu1+"*"+shu2+"=";  gongsi_daan_daan1[1]=daan;  gongsi_daan_daan1[2]=daan1;break;            case 3: shu6=shu1-shu2;daan=String.valueOf(shu6);daan=String.valueOf(shu6);            daan=String.valueOf(shu6);daan1=daan;            gongsi_daan_daan1[0]=shu1+"-"+shu2+"=";  gongsi_daan_daan1[1]=daan;  gongsi_daan_daan1[2]=daan1;break;                                }                return gongsi_daan_daan1;        }                                public String[] three_num() throws YunSuanFu ,GeShiCuoWu        {        ComeTrueXml text=new ComeTrueXml();         string= text.getXml();                                        isNum = pattern.matcher(string[0]);         if( isNum.matches())         {            int y=Integer.parseInt(string[1]);            if(y<0)            {            throw new YunSuanFu("你设定的运算低于系统设定范围:"+",超出系统的范围!");//抛出异常              }                        else if(y>1000)            {            throw new YunSuanFu("你设定的运算数范围:"+",超出系统的范围!");//抛出异常              }        }        else        {            throw new GeShiCuoWu("你输入设定的数的格式不正确,请重启然后再设定!");//抛出异常          }                    int shu=(int)(Math.random()*10+1);        int shu1=(int)(Math.random()*Integer.parseInt(string[1]));        int shu2=(int)(Math.random()*Integer.parseInt(string[1]));        int shu3=(int)(Math.random()*Integer.parseInt(string[1]));        int shu6;        String daan;        String daan1;        String gongsi_daan_daan1[]=new String[3];                switch(shu)        {            case 1:            shu6=shu1*shu2-shu3;daan=String.valueOf(shu6);daan1=daan;            gongsi_daan_daan1[0]=shu1+"*"+shu2+"-"+shu3+"=";  gongsi_daan_daan1[1]=daan;  gongsi_daan_daan1[2]=daan1;break;            case 2:            shu6=shu1*shu2+shu3;daan=String.valueOf(shu6);daan1=daan;            gongsi_daan_daan1[0]=shu1+"*"+shu2+"+"+shu3+"=";  gongsi_daan_daan1[1]=daan;  gongsi_daan_daan1[2]=daan1;break;            case 3:            shu6=shu1*shu2+shu3;daan=String.valueOf(shu6);daan1=daan;            gongsi_daan_daan1[0]=shu1+"*"+shu2+"+"+shu3+"=";  gongsi_daan_daan1[1]=daan;  gongsi_daan_daan1[2]=daan1;break;            case 4:            shu6=shu1*shu2*shu3;daan=String.valueOf(shu6);daan1=daan;            gongsi_daan_daan1[0]=shu1+"*"+shu2+"*"+shu3+"=";  gongsi_daan_daan1[1]=daan;  gongsi_daan_daan1[2]=daan1;break;            case 5:            shu6=shu1-shu2+shu3;daan=String.valueOf(shu6);daan1=daan;            gongsi_daan_daan1[0]=shu1+"-"+shu2+"+"+shu3+"=";  gongsi_daan_daan1[1]=daan;  gongsi_daan_daan1[2]=daan1;break;            case 6:            shu6=shu1-shu2-shu3;daan=String.valueOf(shu6);daan1=daan;            gongsi_daan_daan1[0]=shu1+"-"+shu2+"-"+shu3+"=";  gongsi_daan_daan1[1]=daan;  gongsi_daan_daan1[2]=daan1;break;            case 7:            shu6=shu1-shu2*shu3;daan=String.valueOf(shu6);daan1=daan;            gongsi_daan_daan1[0]=shu1+"-"+shu2+"*"+shu3+"=";  gongsi_daan_daan1[1]=daan;  gongsi_daan_daan1[2]=daan1;break;            case 8:            shu6=shu1+shu2-shu3;daan=String.valueOf(shu6);daan1=daan;            gongsi_daan_daan1[0]=shu1+"+"+shu2+"-"+shu3+"=";  gongsi_daan_daan1[1]=daan;  gongsi_daan_daan1[2]=daan1;break;            case 9:            shu6=shu1+shu2+shu3;daan=String.valueOf(shu6);daan1=daan;            gongsi_daan_daan1[0]=shu1+"+"+shu2+"+"+shu3+"=";  gongsi_daan_daan1[1]=daan;  gongsi_daan_daan1[2]=daan1;break;            case 10:            shu6=shu1+shu2*shu3;daan=String.valueOf(shu6);daan1=daan;            gongsi_daan_daan1[0]=shu1+"+"+shu2+"*"+shu3+"=";  gongsi_daan_daan1[1]=daan;  gongsi_daan_daan1[2]=daan1;break;        }        return gongsi_daan_daan1;        }                        public String[] four_number()throws YunSuanFu ,GeShiCuoWu        {                        ComeTrueXml text=new ComeTrueXml();             string= text.getXml();                isNum = pattern.matcher(string[0]);             isNum1 = pattern.matcher(string[1]);             if( isNum.matches()&&isNum1.matches())             {                int y=Integer.parseInt(string[1]);                if(y<0)                {                throw new YunSuanFu("你设定的运算低于系统设定范围:"+",超出系统的范围!");//抛出异常                  }                else if(y>1000)                {                throw new YunSuanFu("你设定的运算数超出系统设定范围:"+",超出系统的范围!");//抛出异常                  }            }            else            {                throw new GeShiCuoWu("你输入设定的数的格式不正确,请重启然后再设定!");//抛出异常              }                        int shu=(int)(Math.random()*11+1);            int shu1=(int)(Math.random()*Integer.parseInt(string[1]));            int shu2=(int)(Math.random()*Integer.parseInt(string[1]));                    int shu4=(int)(Math.random()*Integer.parseInt(string[1]));            int shu5=(int)(Math.random()*Integer.parseInt(string[1]));            int shu9=(int)(Math.random()*Integer.parseInt(string[1]));            int shu6;                        String daan;            String daan1;            String gongsi_daan_daan1[]=new String[3];                    switch(shu)        {        case 1:shu6=shu1+shu2+shu4+shu5;        daan=String.valueOf(shu6);daan1=daan;        gongsi_daan_daan1[0]=shu1+"+"+shu2+"+"+shu4+"+"+shu5+"=";  gongsi_daan_daan1[1]=daan;  gongsi_daan_daan1[2]=daan1;break;        case 2:shu6=shu1-shu2-shu4-shu5;        daan=String.valueOf(shu6);daan1=daan;        gongsi_daan_daan1[0]=shu1+"-"+shu2+"-"+shu4+"-"+shu5+"=";  gongsi_daan_daan1[1]=daan;  gongsi_daan_daan1[2]=daan1;break;         case 3:shu6=shu1-shu2-shu4+shu5;        daan=String.valueOf(shu6);daan1=daan;        gongsi_daan_daan1[0]=shu1+"-"+shu2+"-"+shu4+"+"+shu5+"=";  gongsi_daan_daan1[1]=daan;  gongsi_daan_daan1[2]=daan1;break;        case 4:shu6=shu1+shu2-shu4-shu5;        daan=String.valueOf(shu6);daan1=daan;        gongsi_daan_daan1[0]=shu1+"+"+shu2+"-"+shu4+"-"+shu5+"=";  gongsi_daan_daan1[1]=daan;  gongsi_daan_daan1[2]=daan1;break;        case 5:shu6=shu1+shu2-shu4+shu5;        daan=String.valueOf(shu6);daan1=daan;        gongsi_daan_daan1[0]=shu1+"+"+shu2+"-"+shu4+"+"+shu5+"=";  gongsi_daan_daan1[1]=daan;  gongsi_daan_daan1[2]=daan1;break;        case 6:shu6=shu1+shu2-shu4+shu5;        daan=String.valueOf(shu6);daan1=daan;        gongsi_daan_daan1[0]=shu1+"+"+shu2+"-"+shu4+"+"+shu5+"=";  gongsi_daan_daan1[1]=daan;  gongsi_daan_daan1[2]=daan1;break;        case 7:shu6=shu1+shu2-shu4+shu5+shu9;        daan=String.valueOf(shu6);daan1=daan;        gongsi_daan_daan1[0]=shu1+"+"+shu2+"-"+shu4+"+"+shu5+"+"+shu9+"=";  gongsi_daan_daan1[1]=daan;  gongsi_daan_daan1[2]=daan1;break;        case 8:shu6=shu1+shu2-shu4-shu5-shu9;        daan=String.valueOf(shu6);daan1=daan;         gongsi_daan_daan1[0]=shu1+"+"+shu2+"-"+shu4+"-"+shu5+"-"+shu9+"=";  gongsi_daan_daan1[1]=daan;  gongsi_daan_daan1[2]=daan1;break;        case 9:shu6=shu1+shu2+shu4+shu5+shu9;        daan=String.valueOf(shu6);daan1=daan;        gongsi_daan_daan1[0]=shu1+"+"+shu2+"+"+shu4+"+"+shu5+"+"+shu9+"=";  gongsi_daan_daan1[1]=daan;  gongsi_daan_daan1[2]=daan1;break;        case 10:shu6=shu1-shu2-shu4+shu5+shu9;        daan=String.valueOf(shu6);daan1=daan;         gongsi_daan_daan1[0]=shu1+"-"+shu2+"-"+shu4+"+"+shu5+"+"+shu9+"=";  gongsi_daan_daan1[1]=daan;  gongsi_daan_daan1[2]=daan1;break;        case 11:shu6=shu1-shu2-shu4+shu5*shu9;        daan=String.valueOf(shu6);daan1=daan;         gongsi_daan_daan1[0]=shu1+"-"+shu2+"-"+shu4+"+"+shu5+"*"+shu9+"=";  gongsi_daan_daan1[1]=daan;  gongsi_daan_daan1[2]=daan1;break;         }        return  gongsi_daan_daan1;        }            }
package ShiYanwu2;import java.io.File;import java.io.FileOutputStream;import java.util.Scanner;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.parsers.ParserConfigurationException;import javax.xml.transform.OutputKeys;import javax.xml.transform.Transformer;import javax.xml.transform.TransformerFactory;import javax.xml.transform.dom.DOMSource;import javax.xml.transform.stream.StreamResult;import org.w3c.dom.Document;import org.w3c.dom.Element;import org.w3c.dom.Node;import org.w3c.dom.NodeList;  class ComeTrueXml implements ReadXmlProject {              String[] string=new String[2];                          public String[] getXml(){                       File file = new File("src//ShiYanwu2//Demo.xml");              DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();              try{                    DocumentBuilder builder = factory.newDocumentBuilder();                    Document doc = builder.parse(file);                    Element element1 = doc.getDocumentElement();                   // String str = element1.getNodeName();//获取根节点的名称                                       NodeList list = element1.getElementsByTagName("FuHaoGeShu");                    NodeList list1 = element1.getElementsByTagName("ShuZiFanWei");                    Node node = list.item(0);                    Node node1 = list1.item(0);                              string[0] = node.getFirstChild().getNodeValue();                    string[1] = node1.getFirstChild().getNodeValue();                                        } catch (Exception e) {                e.printStackTrace();              }              return string;           }                    public void setXml()        {            int define;            String value1;            String value2;            Scanner in=new Scanner(System.in);            File file = new File("src//ShiYanwu2//Demo.xml");              DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();              try{                    DocumentBuilder builder = factory.newDocumentBuilder();                    Document doc = builder.parse(file);                    Element element1 = doc.getDocumentElement();                    String str = element1.getNodeName();//获取根节点的名称                                   NodeList list = element1.getElementsByTagName("FuHaoGeShu");                    NodeList list1 = element1.getElementsByTagName("ShuZiFanWei");                       Node node = list.item(0);                       Node node1 = list1.item(0);                                                                                      System.out.println("请输入要修改的符号个数:");                 value1=in.next();                 System.out.println("请输入要修改的数值范围:");                 value2=in.next();                 node.setTextContent(value1);                 node1.setTextContent(value2);                 TransformerFactory transformerFactory=TransformerFactory.newInstance();                 Transformer transformer=transformerFactory.newTransformer();                 DOMSource domSource=new DOMSource(doc);                 transformer.setOutputProperty(OutputKeys.ENCODING,"UTF-8");                 StreamResult result=new StreamResult(new FileOutputStream("src//ShiYanwu2//Demo.xml"));                  transformer.transform(domSource, result);              } catch (Exception e) {              e.printStackTrace();              }                      }}
5
2
50
 

 

 
package ShiYanwu2;import java.awt.Color;import javax.swing.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.util.Scanner;import java.util.regex.Matcher;import java.util.regex.Pattern;    public class Jiemian extends JFrame implements ActionListener{    private JPanel mianban;    private JTextField wenben;    private JTextField wenben1;        private JLabel  biaoqian0;    private JLabel  biaoqian1;    private JLabel  biaoqian2;        private JButton button;    private JButton button1;    private JButton button2;    private JButton button3;    private JButton button4;    private JButton button5;    private JFrame frame;        private int timu_num=0;    private static JTextArea text;    private String daan="";    private String daan1="";    private int dadui_timu=0;    private int dacuo_timu=0;        private Pattern pattern= Pattern.compile("[0-9]*");    private Matcher isNum;            private String gongsi[]=new String[3];    private String zifu="";    private String zifu1="";    String[] string=new String[2];        public void actionPerformed(ActionEvent e) {          // TODO Auto-generated method stub                      if(e.getSource()==button){  //容量               zifu=wenben.getText();        if(!zifu.equals(""))        {            isNum = pattern.matcher(zifu);             if( isNum.matches())             {            timu_num= Integer.parseInt(zifu);            if(timu_num>5)            {            JOptionPane.showMessageDialog(frame.getContentPane(), "题目数量超过5题,系统默认为5题!",            "系统信息", JOptionPane.INFORMATION_MESSAGE);                timu_num=5;            }            if(timu_num>0&&timu_num<=5)            {                gongsi1();                timu_num--;            }            if(timu_num<=0)            {            JOptionPane.showMessageDialog(frame.getContentPane(), "输入数字不正确!",                         "系统信息", JOptionPane.INFORMATION_MESSAGE);            }            }            else            {                JOptionPane.showMessageDialog(frame.getContentPane(), "输入存在问题!",                        "系统信息", JOptionPane.INFORMATION_MESSAGE);            }            }        else        {            JOptionPane.showMessageDialog(frame.getContentPane(), "没有输入信息!",                       "系统信息", JOptionPane.INFORMATION_MESSAGE);        }        }                                         else if(e.getSource()==button1){  //价值            zifu1=wenben1.getText();            if(!daan.equals("")||!daan.equals(""))             {                    if(timu_num>=0&&timu_num<=5)        { if(daan.equals(zifu1)||daan1.equals(zifu1))        {            JOptionPane.showMessageDialog(frame.getContentPane(), "正确!", "系统信息", JOptionPane.INFORMATION_MESSAGE);            dadui_timu++;            text.setText("答错"+dacuo_timu+"题"+","+"答对"+dadui_timu+"题");        }    else        {            JOptionPane.showMessageDialog(frame.getContentPane(), "错误!答案为:"+daan, "系统信息", JOptionPane.INFORMATION_MESSAGE);                dacuo_timu++;            text.setText("答错"+dacuo_timu+"题"+","+"答对"+dadui_timu+"题");        }            gongsi1();            timu_num--;        }        else        {            JOptionPane.showMessageDialog(frame.getContentPane(), "题目已答完!", "系统信息", JOptionPane.INFORMATION_MESSAGE);            }                                 }                                           else             {                 JOptionPane.showMessageDialog(frame.getContentPane(), "用户没有正确操作!", "系统信息", JOptionPane.INFORMATION_MESSAGE);                 }        }                else if(e.getSource() == button2 ){
//重置 把他们的值改为空 mianban.setBackground(Color.yellow); } else if(e.getSource() == button3){
//确定 mianban.setBackground(Color.green); } else if(e.getSource() == button4){ mianban.setBackground(Color.pink); gongsi1(); } else if(e.getSource() == button5 ){
//重置 把他们的值改为空 if(timu_num!=0) { gongsi1(); } else { JOptionPane.showMessageDialog(frame.getContentPane(), "没有输入题目数量!", "系统信息", JOptionPane.INFORMATION_MESSAGE); } } } Jiemian() { zhujiemian(); } public void zhujiemian() { ComeTrueXml texta=new ComeTrueXml(); Scanner in=new Scanner(System.in); String define; System.out.println("是否改变算式符号与数的范围?是为1:否为任意字符:"); define=in.next(); if("1".equals(define)) { texta.setXml(); } else { } mianban=new JPanel(); wenben=new JTextField(4); wenben1=new JTextField(4); biaoqian0=new JLabel("题目个数:"); biaoqian1=new JLabel("等待输入公式 "); biaoqian2=new JLabel("背景色:"); button=new JButton("确定"); button1=new JButton("确定"); button2=new JButton("黄色"); button3=new JButton("绿色"); button4=new JButton("粉红色"); button5=new JButton("换题"); button.addActionListener(this); button1.addActionListener(this); button2.addActionListener(this); button3.addActionListener(this); button4.addActionListener(this); button5.addActionListener(this); text=new JTextArea(10,24); frame=new JFrame("四则运算"); mianban.add(biaoqian0); mianban.add(wenben); mianban.add(button); mianban.add(button5); mianban.add(biaoqian1); mianban.add(wenben1); mianban.add(button1); mianban.add(text); mianban.add(text); mianban.add(biaoqian2); mianban.add(button2); mianban.add(button3); mianban.add(button4); frame.add(mianban); frame.setBounds(100,100,272,312); frame.setVisible(true); frame.setResizable(false); } public void gongsi1() { ComeTrueXml text=new ComeTrueXml(); int suijichouqu; Core gongsilei=new Core(); string=text.getXml(); isNum = pattern.matcher(string[0]); if( isNum.matches()) { suijichouqu=Integer.parseInt(string[0]); if(suijichouqu>=1&&suijichouqu<=3) { switch(suijichouqu) { case 1: try{gongsi=gongsilei.two_number(); biaoqian1.setText(gongsi[0]);daan=gongsi[1];daan1=gongsi[2]; break;} catch(YunSuanFu yy){System.out.println(yy.getMessage()); }catch(GeShiCuoWu e){System.out.println(e.getMessage());} case 2:try{ gongsi=gongsilei.three_num(); biaoqian1.setText(gongsi[0]);daan=gongsi[1];daan1=gongsi[2];break;}catch(YunSuanFu yy){System.out.println(yy.getMessage()); yy.printStackTrace(); } catch(GeShiCuoWu e){System.out.println(e.getMessage());} case 3:try{ gongsi=gongsilei.four_number(); biaoqian1.setText(gongsi[0]);daan=gongsi[1];daan1=gongsi[2];break;} catch(YunSuanFu yy){System.out.println(yy.getMessage()); }catch(GeShiCuoWu e){System.out.println(e.getMessage());} } } else { JOptionPane.showMessageDialog(frame.getContentPane(), "数据超出范围!", "系统信息", JOptionPane.INFORMATION_MESSAGE); } } else { JOptionPane.showMessageDialog(frame.getContentPane(), "设定的符号数目或者格式存在问题!", "系统信息", JOptionPane.INFORMATION_MESSAGE); } } public static void main(String[]args) { new Jiemian(); }}
package ShiYanwu2;class YunSuanFu extends Exception  {      public YunSuanFu(String mistake)      {      super(mistake);      }  }   class GeShiCuoWu extends Exception {      public GeShiCuoWu(String mistake)      {      super(mistake);      }  }
package ShiYanwu2;  public  interface ReadXmlProject {    String[] getXml();    void setXml();}

 

 1.自定义异常

 

2.接口

package ShiYanwu2;  public  interface ReadXmlProject {    String[] getXml();    void setXml();}

3.一开始要通过接口设置参数

4.假如输入数据出错,自定义的异常会捕捉异常,在四则运算界面运行的时候,因为系统设定数不能超过1000

转载于:https://www.cnblogs.com/shaojiawen/p/4488508.html

你可能感兴趣的文章
我的firefox插件开发历程
查看>>
我很高兴找了一张可以说明:为什么软件开发那么困难的图
查看>>
iOS:翻页效果
查看>>
(原创)Python文件与文件系统系列(5)——stat模块
查看>>
【ABAP】Cross client master/business data transfer guide(ALE &I Doc)
查看>>
一个中型项目:本地校园App
查看>>
BZOJ2809:[Apio2012]dispatching——题解
查看>>
WEBSHELL
查看>>
[转] 残差网络
查看>>
个人作业——软件工程实践总结作业
查看>>
[转载]依赖注入那些事
查看>>
学习笔记===《用户体验要素——以用户为中心的产品设计》
查看>>
CentOS搭建Git服务器
查看>>
多线程篇六:线程池
查看>>
easyui tab页面关闭根据回调函数刷新父tab页
查看>>
GPS围栏两个多边形相交问题的奇葩解法
查看>>
PHPstorm如何导入字体主题
查看>>
静态链表
查看>>
在VS中手工创建一个最简单的WPF程序
查看>>
python for 格式化字符串 list.count
查看>>