zoukankan      html  css  js  c++  java
  • 作业 5.1

      队友:吴哲永

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

      初步实现第一阶段目标。

    主类:

     1 package 四则运算;
     2 
     3 public class Test {
     4 
     5     public static void main(String[] args) {
     6         
     7         MyFrame a=new MyFrame();
     8 
     9     }
    10 
    11 }

    窗体类:

      1 package 四则运算;
      2 
      3 import java.*;
      4 import java.util.*;
      5 
      6 import javax.swing.*;
      7 
      8 import java.awt.*;
      9 import java.awt.event.*;
     10 import java.io.*;
     11 
     12 public class MyFrame extends JFrame
     13 {
     14     JLabel L1=new JLabel("请输入题数(最多不超过5题):");
     15     JLabel L2=new JLabel("第一题:");
     16     JLabel L3=new JLabel("   =   ");
     17     JLabel L4=new JLabel("第二题:");
     18     JLabel L5=new JLabel("   =   ");
     19     JLabel L6=new JLabel("第三题:");
     20     JLabel L7=new JLabel("   =   ");
     21     JLabel L8=new JLabel("第四题:");
     22     JLabel L9=new JLabel("   =   ");
     23     JLabel L10=new JLabel("第五题:");
     24     JLabel L11=new JLabel("   =   ");
     25     JLabel L12=new JLabel("欢迎您,尊敬的 ");
     26     JLabel L13=new JLabel("用户");
     27     JLabel L14=new JLabel("");
     28     JLabel L15=new JLabel("           ");
     29     JLabel L16=new JLabel("   ");
     30     JLabel L17=new JLabel("   ");
     31     JLabel L18=new JLabel("");
     32     JLabel L19=new JLabel("                               ");
     33     JTextField[]T={
     34             new JTextField(10),new JTextField(10),new JTextField(10),new JTextField(10),new JTextField(10),new JTextField(10),new JTextField(10),new JTextField(10),new JTextField(10),new JTextField(10),new JTextField(5)
     35         };
     36     JButton B1=new JButton("开始答题");
     37     JButton B2=new JButton("提交");
     38     
     39     JFrame f=new JFrame();
     40     
     41     String username=new String();
     42     int i;
     43     int k;
     44     int n;
     45     long begintime;
     46     long endtime;
     47     long time;
     48     int[]a=new int[5];
     49     int[]b=new int[5];
     50     int[]c=new int[5];
     51     int[]d=new int[5];
     52     int[]answer=new int[5];
     53     
     54     
     55     public MyFrame() 
     56     {
     57         getContentPane().setBackground(Color.WHITE);
     58         this.setVisible(true);
     59         this.setTitle("四则运算答题");
     60         Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
     61         setBounds((d.width - 502) / 2, (d.height - 450) / 2, 502, 450);
     62         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     63         this.setResizable(false);
     64         getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER, 12, 10));
     65         L12.setForeground(Color.RED);
     66         L12.setFont(new Font("宋体", Font.BOLD, 25));
     67         getContentPane().add(L12);
     68         L13.setForeground(SystemColor.textHighlight);
     69         L13.setFont(new Font("宋体", Font.BOLD, 25));
     70         getContentPane().add(L13);
     71         L1.setFont(new Font("宋体", Font.PLAIN, 20));
     72         getContentPane().add(L1);
     73         T[10].setFont(new Font("宋体", Font.PLAIN, 20));
     74         getContentPane().add(T[10]);
     75         B1.setFont(new Font("宋体", Font.PLAIN, 20));
     76         getContentPane().add(B1);
     77         L2.setFont(new Font("宋体", Font.PLAIN, 20));
     78         getContentPane().add(L2);
     79         T[0].setFont(new Font("宋体", Font.PLAIN, 20));
     80         getContentPane().add(T[0]);
     81         L3.setFont(new Font("宋体", Font.PLAIN, 20));
     82         getContentPane().add(L3);
     83         T[5].setFont(new Font("宋体", Font.PLAIN, 20));
     84         getContentPane().add(T[5]);
     85         L4.setFont(new Font("宋体", Font.PLAIN, 20));
     86         getContentPane().add(L4);
     87         T[1].setFont(new Font("宋体", Font.PLAIN, 20));
     88         getContentPane().add(T[1]);
     89         L5.setFont(new Font("宋体", Font.PLAIN, 20));
     90         getContentPane().add(L5);
     91         T[6].setFont(new Font("宋体", Font.PLAIN, 20));
     92         getContentPane().add(T[6]);
     93         L6.setFont(new Font("宋体", Font.PLAIN, 20));
     94         getContentPane().add(L6);
     95         T[2].setFont(new Font("宋体", Font.PLAIN, 20));
     96         getContentPane().add(T[2]);
     97         L7.setFont(new Font("宋体", Font.PLAIN, 20));
     98         getContentPane().add(L7);
     99         T[7].setFont(new Font("宋体", Font.PLAIN, 20));
    100         getContentPane().add(T[7]);
    101         L8.setFont(new Font("宋体", Font.PLAIN, 20));
    102         getContentPane().add(L8);
    103         T[3].setFont(new Font("宋体", Font.PLAIN, 20));
    104         getContentPane().add(T[3]);
    105         L9.setFont(new Font("宋体", Font.PLAIN, 20));
    106         getContentPane().add(L9);
    107         T[8].setFont(new Font("宋体", Font.PLAIN, 20));
    108         getContentPane().add(T[8]);
    109         L10.setFont(new Font("宋体", Font.PLAIN, 20));
    110         getContentPane().add(L10);
    111         T[4].setFont(new Font("宋体", Font.PLAIN, 20));
    112         getContentPane().add(T[4]);
    113         L11.setFont(new Font("宋体", Font.PLAIN, 20));
    114         getContentPane().add(L11);
    115         T[9].setFont(new Font("宋体", Font.PLAIN, 20));
    116         getContentPane().add(T[9]);
    117         L14.setFont(new Font("宋体", Font.PLAIN, 20));
    118         getContentPane().add(L14);
    119         L19.setFont(new Font("宋体", Font.PLAIN, 20));
    120         getContentPane().add(L19);
    121         B2.setFont(new Font("宋体", Font.PLAIN, 20));
    122         getContentPane().add(B2);
    123         L15.setFont(new Font("宋体", Font.PLAIN, 20));
    124         getContentPane().add(L15);
    125         L16.setFont(new Font("宋体", Font.PLAIN, 20));
    126         getContentPane().add(L16);
    127         L14.setFont(new Font("宋体", Font.PLAIN, 20));
    128         getContentPane().add(L14);
    129         L17.setFont(new Font("宋体", Font.PLAIN, 20));
    130         getContentPane().add(L17);
    131         L18.setFont(new Font("宋体", Font.PLAIN, 20));
    132         getContentPane().add(L18);
    133         
    134         File file=new File("用户名.txt");
    135         if(!file.exists())
    136         {
    137             try
    138              {
    139                 file.createNewFile();
    140             }
    141             catch (IOException e)
    142             {
    143                 e.printStackTrace();
    144             }
    145             Inputname b=new Inputname();
    146         }
    147         else
    148         {
    149             String str1=new String("");; 
    150             int i=0;
    151             BufferedReader br = null;
    152             try 
    153             {
    154                 br = new BufferedReader(new FileReader(file.getName()));
    155             } 
    156             catch (FileNotFoundException e1) 
    157             {
    158                 e1.printStackTrace();
    159             }
    160             try 
    161             {
    162                 while((str1=br.readLine())!=null)
    163                 {
    164                     username=str1;
    165                 }
    166             }
    167             catch (IOException e)
    168             {
    169                 e.printStackTrace();
    170             }
    171             try 
    172             {
    173                 br.close();
    174             } 
    175             catch (IOException e) 
    176             {
    177                 e.printStackTrace();
    178             }
    179             if(username.length()==0)
    180             {
    181                 Inputname b=new Inputname();
    182             }
    183             else
    184             {
    185                 L13.setText(username);
    186             }
    187         }
    188         
    189         B1.addActionListener(new Action());
    190         B2.addActionListener(new Action());
    191     }
    192     
    193     public class Action implements ActionListener
    194     {
    195 
    196         public void actionPerformed(ActionEvent e) 
    197         {
    198             try
    199             {
    200                 if(e.getSource()==B1)
    201                 {
    202                     for(k=0;k<10;k++)
    203                     {
    204                         T[k].setText(null);
    205                     }
    206                     L15.setText("           ");
    207                     L16.setText(null);
    208                     L17.setText(null);
    209                     L18.setText("");
    210                     i=0;
    211                     
    212                     if(T[10].getText().length()==0)
    213                     {
    214                         JOptionPane.showMessageDialog(f,"请输入题数!");
    215                     }
    216                     else if(Integer.parseInt(T[10].getText())>5)
    217                     {
    218                         JOptionPane.showMessageDialog(f,"题数最多不超过5!");
    219                     }
    220                     else
    221                     {
    222                         i=Integer.parseInt(T[10].getText());
    223                         
    224                         for(k=0;k<i;k++)
    225                         {
    226                             a[k]=(int)(Math.random()*100+1);
    227                             b[k]=(int)(Math.random()*100+1);
    228                             c[k]=(int)(Math.random()*5);
    229                             d[k]=(int)(Math.random()*12+1);
    230                             Core core=new Core(a[k],b[k],c[k],d[k]);
    231                             answer[k]=core.calc();
    232                             T[k].setText(core.toString());
    233                             
    234                         }
    235                         begintime = System.currentTimeMillis();
    236                     }
    237                 }
    238             }catch(NumberFormatException u)
    239             {
    240                 JOptionPane.showMessageDialog(f,"请输入正确的数字!");
    241             }
    242             if(e.getSource()==B2)
    243             {
    244                 if(i==0)
    245                 {
    246                     JOptionPane.showMessageDialog(f,"请先进行答题!");
    247                 }
    248                 else
    249                 {
    250                     endtime = System.currentTimeMillis();
    251                     time=(endtime-begintime)/1000;
    252                     n=0;
    253                     for(k=0;k<i;k++)
    254                     {
    255                         if(T[k+5].getText().length()==0)
    256                         {
    257                             continue;
    258                         }
    259                         try
    260                         {
    261                             if(answer[k]==Integer.parseInt(T[k+5].getText()))
    262                             {
    263                                 n++;
    264                             }
    265                         }catch(NumberFormatException u)
    266                         {
    267                             JOptionPane.showMessageDialog(f,"请输入正确的数字!");
    268                         }
    269                         
    270                     }
    271                     L15.setText("你答对了:"+n+"道题");
    272                     L16.setText("你答错了:"+(i-n)+"道题");
    273                     L14.setText("用时:"+time+"秒");
    274                     L17.setText("参考答案:");
    275                     for(k=0;k<i;k++)
    276                     {
    277                         L18.setText(L18.getText()+answer[k]+";");
    278                     }
    279                     i=0;
    280                 }
    281             }
    282         }
    283         
    284         
    285     }
    286     
    287     public class Inputname extends JFrame
    288     {
    289         JLabel L1=new JLabel("请输入您的用户名:");
    290         JTextField T1=new JTextField(17);
    291         JButton B1=new JButton("确定");
    292         JButton B2=new JButton("关闭");
    293         
    294         JFrame f=new JFrame();
    295         
    296         public Inputname()
    297         {
    298             this.setVisible(true);
    299             this.setTitle("输入用户名");
    300             Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
    301             setBounds((d.width - 241) / 2, (d.height - 138) / 2, 242, 146);
    302             setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    303             this.setResizable(false);
    304             getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER, 20, 6));
    305             L1.setFont(new Font("宋体", Font.PLAIN, 20));
    306             getContentPane().add(L1);
    307             T1.setFont(new Font("宋体", Font.PLAIN, 20));
    308             getContentPane().add(T1);
    309             B1.setFont(new Font("宋体", Font.PLAIN, 20));
    310             getContentPane().add(B1);
    311             B2.setFont(new Font("宋体", Font.PLAIN, 20));
    312             getContentPane().add(B2);
    313             B1.addActionListener(new action());
    314             B2.addActionListener(new action());
    315         }
    316         
    317         public class action implements ActionListener
    318         {
    319 
    320             public void actionPerformed(ActionEvent e)
    321             {
    322                 if(e.getSource()==B2)
    323                 {
    324                     setVisible(false);
    325                 }
    326                 
    327                 if(e.getSource()==B1)
    328                 {
    329                     if(T1.getText().length()==0)
    330                     {
    331                         JOptionPane.showMessageDialog(f,"用户名不能为空");
    332                     }
    333                     else
    334                     {
    335                         username=T1.getText();
    336                         L13.setText(username);
    337                         
    338                         File file=new File("用户名.txt");
    339                         try{
    340                         BufferedWriter writer=new BufferedWriter(new FileWriter(file.getName()));
    341                             writer.write(username);;
    342                         writer.close();
    343                         } catch(IOException g)
    344                         {
    345                             System.out.print(g);
    346                         }
    347                         
    348                         setVisible(false);
    349                     }
    350                     
    351                 }
    352                 
    353             }
    354             
    355         }
    356 
    357     }
    358     
    359 }

    包含计算功能的核心类:

     1 package 四则运算;
     2 
     3 public class Core {
     4     int a;
     5     int b;
     6     int c;
     7     int d;
     8     public Core(int a,int b,int c,int d)
     9     {
    10         this.a=a;
    11         this.b=b;
    12         this.c=c;
    13         this.d=d;
    14     }
    15     public int calc()
    16     {
    17         if(c==0)
    18         {
    19             return a+b;
    20         }
    21         else if(c==1)
    22         {
    23             return a-b;
    24         }
    25         else if(c==2)
    26         {
    27             return a*b;
    28         }
    29         else if(c==3)
    30         {
    31             return a/b;
    32         }
    33         else
    34         {
    35             return jiecheng(d);
    36         }
    37     }
    38     public String toString()
    39     {
    40         if(c==0)
    41         {
    42             return a+"+"+b;
    43         }
    44         else if(c==1)
    45         {
    46             return a+"-"+b;
    47         }
    48         else if(c==2)
    49         {
    50             return a+"*"+b;
    51         }
    52         else if(c==3)
    53         {
    54             return a+"/"+b;
    55         }
    56         else
    57         {
    58             return d+"!";
    59         }
    60     }
    61 
    62     public int jiecheng(int x)
    63     {
    64         if(x==0 || x==1)
    65         {
    66             return 1;
    67         }
    68         else
    69         {
    70             return x*jiecheng(x-1);
    71         }
    72     }
    73 }
  • 相关阅读:
    【linux】在命令行里进行数学计算
    vi编辑器的分割窗口
    Linux 磁盘管理命令
    【Matlab】sparse函数和full函数(稀疏矩阵和非稀疏矩阵转换)
    【R】 绘制 热图 heatmap
    Intent用法实例
    Android调用天气预报的WebService简单例子
    Broadcast广播消息
    Android与服务器端数据交互(基于SOAP协议整合android+webservice)
    Java中String类的方法及说明
  • 原文地址:https://www.cnblogs.com/xujiahao/p/4468628.html
Copyright © 2011-2022 走看看