zoukankan      html  css  js  c++  java
  • 作业2 结对子作业

     一、这次是两人合作做的作业,小伙伴的名字及博客地址是:江丹仪,http://www.cnblogs.com/JDY64/

     二、我们一共用了一天左右的时间去编写这个程序

     三、运行环境:Eclipse

     四、我们做的4个方向有:

    1.程序可以出带括号的正整数四则运算,支持分数,除法保留两位小数,如:(1/3+1)*2 = 2.67,特别注意:这里是2.67而非2.66,或保持分数形式:8/3;(还未完成,待定)

    2.用户答题结束以后,程序可以显示用户答题所用的时间;(还未完成,待定)

    3.用户可以选择出题的个数(最多不能超过5个题目),答题结束可以显示用户答错的题目个数和答对的题目个数;

    4.程序可以设置皮肤功能,可以改变界面的颜色即可。

    五、分工合作:我做了界面窗体,小伙伴做了算法以及对多个符号运行的情况。

    六、我们遇到的问题主要是:

    1.一开始的运算符打印不出,后来经过同学帮助,才知道要将其转换成String字符型;

    2.还有在生成多个运算符号的时候,只会用最笨的方法来解决,至今没找到更为方便的方法,但我想这是我们比较创新的想法;

    3.在验证正确答案时因为运算符是随机产生,一开始也不知如何进行,后来用if解决了;

    4.按钮事件的监听里面不能使用for循环,试了很多次都执行不了,也不知道问题出在哪里,后来就去掉了for循环直接用if语句;

     七、代码如下: 

      1 import java.awt.Dimension;
      2 import java.awt.Toolkit;
      3 import java.awt.event.ActionEvent;
      4 import java.awt.event.ActionListener;
      5 import java.util.Random;
      6 import java.util.Scanner;
      7 import javax.swing.JButton;
      8 import javax.swing.JFrame;
      9 import javax.swing.JLabel;
     10 import javax.swing.JOptionPane;
     11 import javax.swing.JPanel;
     12 import javax.swing.JTextField;
     13 import java.awt.Color;
     14 public class yun {
     15     public static void main(String[] args) {
     16         new Calculator();
     17     }
     18 }
     19 
     20 class Calculator extends JFrame{
     21     private JLabel L1,L2,L3,L4;
     22     //private JLabel L5;
     23     private JTextField T1,T2,T3,T4,T5;
     24     private JButton B1,B2,B3;//按钮
     25     int a,b,c,d,h;
     26     char fh2,fh3,fh4;
     27     char[]fh={'+','-','*','/'};
     28     Scanner in=new Scanner(System.in);
     29     int ranswer,answer;//正确答案和用户输入答案
     30     int num,fhnum;//出题数和符号数
     31     int wrong=0,right=0;//对错题数
     32     int score=0;
     33     int sum=0;
     34     
     35     public Calculator(){
     36         this("四则运算");
     37     }
     38     
     39     public Calculator(String title){
     40         super(title);//设置窗体标题
     41         setSize(510,150);//设置窗体大小
     42         //屏幕居中显示
     43         Dimension size=Toolkit.getDefaultToolkit().getScreenSize();//获得屏幕的标尺
     44         int screenWidth=size.width;
     45         int screenHeight=size.height;
     46         int x=(screenWidth-this.getWidth())/2;
     47         int y=(screenHeight-this.getHeight())/2;
     48         this.setLocation(x, y);
     49         //设置关闭按钮操作
     50         this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
     51         setResizable(false);//设置窗体大小不可改变
     52         this.setVisible(true);//设置窗体可见
     53         
     54         //创建界面元素
     55         L1=new JLabel("选择所需的题目数(1-5):");
     56         T1=new JTextField(10);   //放题目个数
     57         L2=new JLabel("运算符的个数:");
     58         T2=new JTextField(10);   //放运算符个数
     59         B1=new JButton("开始");
     60         L3=new JLabel("四则运算式:");
     61         T3=new JTextField(10);  //设置运算式
     62         L4=new JLabel("输入答案:");
     63         T4=new JTextField(10);  //获取用户输入答案
     64         B2=new JButton("下一题");
     65         T5=new JTextField(10);//获取正确答案
     66         B3=new JButton("确定");
     67         //L5=new JLabel("计时");
     68         
     69         
     70         //创建面板
     71         JPanel p2=new JPanel();
     72         JPanel p1=new JPanel();
     73         p2.add(L1);
     74         p2.add(T1);
     75         p2.add(L2);
     76         p2.add(T2);
     77         p2.add(L3);
     78         p2.add(T3);
     79         p2.add(L4);
     80         p2.add(T4);
     81        // p2.add(L5);
     82         p1.add(B1);
     83         p1.add(B2);
     84         p1.add(B3);
     85         add(p2,"Center");
     86         add(p1,"South");
     87         
     88       //设置窗体界面颜色
     89         p1.setBackground(Color.gray);
     90         p2.setBackground(Color.gray);
     91         
     92         B1.addActionListener(new ActionListener(){
     93             public void actionPerformed(ActionEvent e) {
     94                 num=Integer.parseInt(T1.getText());
     95                 fhnum=Integer.parseInt(T2.getText());
     96                 sum++;
     97                 a=new Random().nextInt(100);
     98                 b=new Random().nextInt(100);
     99                 c=new Random().nextInt(100);
    100                 d=new Random().nextInt(100);
    101                 h=new Random().nextInt(4);
    102                 fh2=fh[h];
    103                 fh3=fh[h];
    104                 fh4=fh[h];
    105                 if(num>5)
    106                 {
    107                     JOptionPane.showMessageDialog(null, "输入有误,请重新输入!");
    108                     
    109                 }
    110                 if(num<=5){
    111                     
    112                 
    113                 
    114                 if(fhnum==1)//一个运算符
    115                 {
    116                 T3.setText(a+String.valueOf(fh2)+b+"=");    
    117                 if(fh2=='+'){
    118                     ranswer=a+b;
    119                 }
    120                 if(fh2=='-'){
    121                     ranswer=a-b;
    122                 }
    123                 
    124                 if(fh2=='*'){
    125                     ranswer=a*b;
    126                 }
    127                 if(fh2=='/'){
    128                     ranswer=a/b;
    129                 }
    130                 }
    131             
    132             
    133             
    134                 if(fhnum==2)//两个运算符
    135                 {
    136                 T3.setText(a+String.valueOf(fh2)+b+String.valueOf(fh3)+c+"=");
    137                 if(fh2=='+')
    138                 {
    139                 if(fh3=='+'){
    140                     ranswer=a+b+c;
    141                 }
    142                 if(fh3=='-'){
    143                     ranswer=a+b-c;
    144                 }
    145                 
    146                 if(fh3=='*'){
    147                     ranswer=a+b*c;
    148                 }
    149                 if(fh3=='/'){
    150                     ranswer=a+b/c;
    151                 }
    152             }
    153                 if(fh2=='-')
    154                 {
    155                 if(fh3=='+'){
    156                     ranswer=a-b+c;
    157                 }
    158                 if(fh3=='-'){
    159                     ranswer=a-b-c;
    160                 }
    161                 
    162                 if(fh3=='*'){
    163                     ranswer=a-b*c;
    164                 }
    165                 if(fh3=='/'){
    166                     ranswer=a-b/c;
    167                 }        
    168             }
    169             
    170             if(fh2=='*')
    171             {
    172                 if(fh3=='+'){
    173                     ranswer=a*b+c;
    174                 }
    175                 if(fh3=='-'){
    176                     ranswer=a*b-c;
    177                 }
    178                 
    179                 if(fh3=='*'){
    180                     ranswer=a*b*c;
    181                 }
    182                 if(fh3=='/'){
    183                     ranswer=a*b/c;
    184                 }
    185             }
    186             if(fh2=='/')
    187             {
    188                 if(fh3=='+'){
    189                     ranswer=a/b+c;
    190                 }
    191                 if(fh3=='-'){
    192                     ranswer=a/b-c;
    193                 }
    194                 
    195                 if(fh3=='*'){
    196                     ranswer=a/b*c;
    197                 }
    198                 if(fh3=='/'){
    199                     ranswer=a-b/c;
    200                 }
    201             }
    202                 }
    203                 
    204                 if(fhnum==3)//三个运算符
    205                 {
    206                 T3.setText(a+String.valueOf(fh2)+b+String.valueOf(fh3)+c+String.valueOf(fh3)+d+"=");
    207                 if(fh2=='+')//第一为加
    208                     {
    209                         if(fh3=='+')
    210                         {
    211                             if(fh4=='+')
    212                             {ranswer=a+b+c+d;}
    213                             if(fh4=='-')
    214                             {ranswer=a+b+c-d;}
    215                             if(fh4=='*')
    216                             {ranswer=a+b+c*d;}
    217                             if(fh4=='/')
    218                             {ranswer=a+b+c/d;}
    219                             
    220                         }
    221                         if(fh3=='-')
    222                         {
    223                             if(fh4=='+')
    224                             {ranswer=a+b-c+d;}
    225                             if(fh4=='-')
    226                             {ranswer=a+b-c-d;}
    227                             if(fh4=='*')
    228                             {ranswer=a+b-c*d;}
    229                             if(fh4=='/')
    230                             {ranswer=a+b-c/d;}
    231                             
    232                         }
    233                         if(fh3=='*')
    234                         {
    235                             if(fh4=='+')
    236                             {ranswer=a+b*c+d;}
    237                             if(fh4=='-')
    238                             {ranswer=a+b*c-d;}
    239                             if(fh4=='*')
    240                             {ranswer=a+b*c*d;}
    241                             if(fh4=='/')
    242                             {ranswer=a+b*c/d;}
    243                             
    244                         }
    245                         if(fh3=='/')
    246                         {
    247                             if(fh4=='+')
    248                             {ranswer=a+b/c+d;}
    249                             if(fh4=='-')
    250                             {ranswer=a+b/c-d;}
    251                             if(fh4=='*')
    252                             {ranswer=a+b/c*d;}
    253                             if(fh4=='/')
    254                             {ranswer=a+b/c/d;}
    255                             
    256                         }
    257                     }
    258                     
    259                     
    260                     if(fh2=='-')//第一为减
    261                     {
    262                         if(fh3=='+')
    263                         {
    264                             if(fh4=='+')
    265                             {ranswer=a-b+c+d;}
    266                             if(fh4=='-')
    267                             {ranswer=a-b+c-d;}
    268                             if(fh4=='*')
    269                             {ranswer=a-b+c*d;}
    270                             if(fh4=='/')
    271                             {ranswer=a-b+c/d;}
    272                             
    273                         }
    274                         if(fh3=='-')
    275                         {
    276                             if(fh4=='+')
    277                             {ranswer=a-b-c+d;}
    278                             if(fh4=='-')
    279                             {ranswer=a-b-c-d;}
    280                             if(fh4=='*')
    281                             {ranswer=a-b-c*d;}
    282                             if(fh4=='/')
    283                             {ranswer=a-b-c/d;}
    284                             
    285                         }
    286                         if(fh3=='*')
    287                         {
    288                             if(fh4=='+')
    289                             {ranswer=a-b*c+d;}
    290                             if(fh4=='-')
    291                             {ranswer=a-b*c-d;}
    292                             if(fh4=='*')
    293                             {ranswer=a-b*c*d;}
    294                             if(fh4=='/')
    295                             {ranswer=a-b*c/d;}
    296                             
    297                         }
    298                         if(fh3=='/')
    299                         {
    300                             if(fh4=='+')
    301                             {ranswer=a-b/c+d;}
    302                             if(fh4=='-')
    303                             {ranswer=a-b/c-d;}
    304                             if(fh4=='*')
    305                             {ranswer=a-b/c*d;}
    306                             if(fh4=='/')
    307                             {ranswer=a-b/c/d;}
    308                             
    309                         }
    310                     }
    311                     if(fh2=='*')//第一为乘
    312                     {
    313                         if(fh3=='+')
    314                         {
    315                             if(fh4=='+')
    316                             {ranswer=a*b+c+d;}
    317                             if(fh4=='-')
    318                             {ranswer=a*b+c-d;}
    319                             if(fh4=='*')
    320                             {ranswer=a*b+c*d;}
    321                             if(fh4=='/')
    322                             {ranswer=a*b+c/d;}
    323                             
    324                         }
    325                         if(fh3=='-')
    326                         {
    327                             if(fh4=='+')
    328                             {ranswer=a*b-c+d;}
    329                             if(fh4=='-')
    330                             {ranswer=a*b-c-d;}
    331                             if(fh4=='*')
    332                             {ranswer=a*b-c*d;}
    333                             if(fh4=='/')
    334                             {ranswer=a*b-c/d;}
    335                             
    336                         }
    337                         if(fh3=='*')
    338                         {
    339                             if(fh4=='+')
    340                             {ranswer=a*b*c+d;}
    341                             if(fh4=='-')
    342                             {ranswer=a*b*c-d;}
    343                             if(fh4=='*')
    344                             {ranswer=a*b*c*d;}
    345                             if(fh4=='/')
    346                             {ranswer=a*b*c/d;}
    347                             
    348                         }
    349                         if(fh3=='/')
    350                         {
    351                             if(fh4=='+')
    352                             {ranswer=a*b/c+d;}
    353                             if(fh4=='-')
    354                             {ranswer=a*b/c-d;}
    355                             if(fh4=='*')
    356                             {ranswer=a*b/c*d;}
    357                             if(fh4=='/')
    358                             {ranswer=a*b/c/d;}
    359                             
    360                         }
    361                     }
    362                 }
    363             }
    364             }
    365         
    366         });
    367         B3.addActionListener(new ActionListener(){
    368             public void actionPerformed(ActionEvent e){
    369                 if(ranswer==Integer.parseInt(T4.getText())){
    370                     JOptionPane.showMessageDialog(null, "恭喜你!答对了");
    371                     right++;
    372                 }
    373                 if(ranswer!=Integer.parseInt(T4.getText())){
    374                     JOptionPane.showMessageDialog(null,"很遗憾!答错了!正确答案是"+ranswer);
    375                     wrong++;
    376                 }
    377             }
    378         });
    379         
    380         B2.addActionListener(new ActionListener(){
    381             public void actionPerformed(ActionEvent e) {
    382                 if(sum==num)
    383                 {
    384                     JOptionPane.showMessageDialog(null, "答题结束!答对了"+right+",答错了"+wrong);
    385                     System.exit(0);
    386                 }
    387                 else
    388                 {
    389                     T3.setText(null);
    390                     T4.setText(null);
    391                 }
    392             }
    393         });
    394 }
    395 }

    八、运行结果:

    1.开始界面:

     2.若输出的题目数超过5个,则如下:

    3.输入了题目数和运算符个数后,按“开始”按钮,则如下:

    4.输入正确答案后,按“确定”按钮,则如下:

    5.按完“确定”按钮后,按“下一题”按钮就会清空“四则运算式”和“输入答案”的文本框,如下:

    6.再按”开始“按钮就会再次随机产生一个新的运算式,如下:

    7.输入错误答案,则会显示如下:

    8.然后再按“下一题”按钮,由于题目已出完,则如下:

    9.当用户输入的运算符为2个时,则如下:

    10.当用户输入的运算符为3个时,则如下:

    九、两人合作时的照片:

  • 相关阅读:
    c++ builder 获取命令行参数
    c++ builder 只允许程序运行一个实例
    jQuery学习笔记(三)
    jQuery学习笔记(二)
    jQuery实现一个弹出登陆层的效果
    jQuery学习笔记(一)
    20117月
    201112学习
    21125
    211211
  • 原文地址:https://www.cnblogs.com/babybluecsj/p/4420462.html
Copyright © 2011-2022 走看看