zoukankan      html  css  js  c++  java
  • 四则运算3

    要求:

    程序能够接受用户输入答案,并判断对错,最后输出对错个数

    一、解题思路

    1、在输出算术题的时候加入用户输入语句,并用if语句来判断对错。

    2、新建一个二维数组b[][],用count1,count2分别对错题和对题的进行计数,并将错误的题放入数组b[][]中。

    3、将数组b[][]内的错题输出。

    二、程序源码

      1 package szys;
      2 import java.util.Scanner;
      3 import java.util.Random;
      4 public class szys2 
      5 {
      6 
      7     public static void main(String[] args) 
      8     {
      9         // TODO 自动生成的方法存根
     10         System.out.println("********四则运算*********");
     11         System.out.println("******1.加减运算*********");
     12         System.out.println("******2.加减乘除运算******");
     13         System.out.println("**********************");
     14         Scanner n=new Scanner(System.in);
     15         System.out.print("请输入运算选项:  ");
     16         int n1=n.nextInt();
     17         switch(n1)
     18         {
     19             case 1:add();
     20                 break;
     21             case 2:mul();
     22                 break;
     23         }
     24     }
     25     
     26     public static void add()
     27     {              //加减法
     28         System.out.println("******加法运算******");
     29         int i,count1=0,count2=0;
     30         char[] ch={'+','-',};//字符数组
     31         int a[][]=new int[10000][3];
     32         int b[][]=new int[10000][4];
     33         Random r=new Random();
     34         Scanner s=new Scanner(System.in);
     35         int n1 = 0;
     36         System.out.print("请输入算术题的个数:  ");
     37         int m1=s.nextInt();
     38         //System.out.print("请输入每行打印的个数:  ");
     39         //int m2=s.nextInt();
     40         System.out.print("加减是否有负数(1/0):");
     41         int c1=s.nextInt();
     42         if(c1==1)
     43         {
     44             System.out.println("请选择数值范围:");
     45             int a1=s.nextInt();
     46             int a2=s.nextInt();
     47             int count=0;
     48             System.out.println("******算数如下******");
     49             while(count<m1)
     50             {
     51                 boolean m=true;
     52                 int s1=(r.nextInt(a2-a1+1)+a1);
     53                 int s2=(r.nextInt(a2-a1+1)+a1);
     54                 int s3=(r.nextInt(ch.length));
     55                 for(i=0;i<=count;i++)
     56                 {
     57                     if(a[i][0]==s1&&a[i][1]==s2&&a[i][2]==s3)
     58                     {
     59                         m=false;
     60                     }
     61                 }
     62                 if(m=true)
     63                 {
     64                     a[count][0]=s1;
     65                     a[count][1]=s2;
     66                     a[count][2]=s3;
     67                 }
     68                 else
     69                 {
     70                     count--;
     71                 }
     72                 count++;
     73             }
     74            for(i=0;i<m1;i++)
     75              {
     76                  switch(a[i][2])
     77                  {
     78                  case 0:{System.out.print(a[i][0]+"+"+a[i][1]+"="+"   ");
     79                  break;}
     80                  case 1:{System.out.print(a[i][0]+"-"+a[i][1]+"="+"   ");
     81                  break;}
     82                  }
     83                  Scanner s1=new Scanner(System.in);
     84                  n1=s.nextInt();
     85                  if(n1==a[i][0]+a[i][1]||n1==a[i][0]-a[i][1])
     86                  {
     87                      count1++;
     88                  }
     89                  else
     90                  {
     91                      b[count2][0]=a[i][0];
     92                      b[count2][1]=a[i][1];
     93                      b[count2][2]=a[i][2];
     94                      b[count2][3]=n1;
     95                      count2++;
     96                  }
     97              }
     98             System.out.println("测试结果如下:");
     99             System.out.println("共"+m1+"道题,答对"+count1+"道题,答错"+count2+"道题");
    100             System.out.println("错题如下:");
    101             for(i=0;i<count2;i++)
    102             {
    103                 switch(b[i][2])
    104                  {
    105                  case 0:{System.out.print(b[i][0]+"+"+b[i][1]+"="+b[i][3]+"   ");
    106                  break;}
    107                  case 1:{System.out.print(b[i][0]+"-"+b[i][1]+"="+b[i][3]+"   ");
    108                  break;}
    109                  }
    110             }
    111         }
    112         else
    113         {
    114             System.out.println("请选测数值范围:");
    115             int a1=s.nextInt();
    116             int a2=s.nextInt();
    117             int count=0;
    118             while(count<m1)
    119             {
    120                 boolean m=true;
    121                 int s1=(r.nextInt(a2-a1+1)+a1);
    122                 int s2=(r.nextInt(a2-a1+1)+a1);
    123                 int s3=(r.nextInt(ch.length));
    124                 for(i=0;i<=count;i++)
    125                 {
    126                     if(a[i][0]==s1&&a[i][1]==s2&&a[i][2]==s3)
    127                     {
    128                         m=false;
    129                     }
    130                 }
    131                 if(m=true)
    132                 {
    133                     a[count][0]=s1;
    134                     a[count][1]=s2;
    135                     a[count][2]=s3;
    136                 }
    137                 else
    138                 {
    139                     count--;
    140                 }
    141                 count++;
    142             }
    143             for(i=0;i<m1;i++)
    144              {
    145                  switch(a[i][2])
    146                  {
    147                  case 0:{System.out.print(a[i][0]+"+"+a[i][1]+"="+"   ");
    148                  break;}
    149                  case 1:{System.out.print(a[i][0]+"-"+a[i][1]+"="+"   ");
    150                  break;}
    151                  }
    152                  n1=s.nextInt();
    153                  if(n1==a[i][0]+a[i][1]||n1==a[i][0]-a[i][1])
    154                  {
    155                      count1++;
    156                  }
    157                  else
    158                  {
    159                      b[count2][0]=a[i][0];
    160                      b[count2][1]=a[i][1];
    161                      b[count2][2]=a[i][2];
    162                      b[count2][3]=n1;
    163                      count2++;
    164                  }
    165              }
    166             System.out.println("测试结果如下:");
    167             System.out.println("共"+m1+"道题,答对"+count1+"道题,答错"+count2+"道题");
    168             System.out.println("错题如下:");
    169             for(i=0;i<count2;i++)
    170             {
    171                 switch(b[i][2])
    172                  {
    173                  case 0:{System.out.print(b[i][0]+"+"+b[i][1]+"="+b[i][3]+"   ");
    174                  break;}
    175                  case 1:{System.out.print(b[i][0]+"-"+b[i][1]+"="+b[i][3]+"   ");
    176                  break;}
    177                  }
    178             }
    179         }
    180     }
    181     
    182     public static void mul()
    183     {              //乘除法
    184         System.out.println("******乘除法运算******");
    185         int i,count1=0,count2=0;
    186         Random r=new Random(); 
    187         char[] ch={'+','-','*','/'};//字符数组
    188         int a[][]=new int[10000][3];
    189         int b[][]=new int[10000][4];
    190         Scanner s=new Scanner(System.in);
    191         int n1 = 0;
    192         System.out.print("请输入算术题的个数:  ");
    193         int m1=s.nextInt();
    194         //System.out.print("请输入每行打印的个数:  ");
    195         //int m3=s.nextInt();
    196         System.out.print("加减是否有负数(1/0):");
    197         int c2=s.nextInt();
    198         if(c2==1)
    199         {
    200             System.out.println("请选测数值范围:");
    201             int a1=s.nextInt();
    202             int a2=s.nextInt();
    203             int count=0;
    204             while(count<m1)
    205             {
    206                 boolean m=true;
    207                 int s1=(r.nextInt(a2-a1+1)+a1);
    208                 int s2=(r.nextInt(a2-a1+1)+a1);
    209                 int s3=(r.nextInt(ch.length));
    210                 for(i=0;i<=count;i++)
    211                 {
    212                     if(a[i][0]==s1&&a[i][1]==s2&&a[i][2]==s3)
    213                     {
    214                         m=false;
    215                     }
    216                 }
    217                 if(m=true)
    218                 {
    219                     a[count][0]=s1;
    220                     a[count][1]=s2;
    221                     a[count][2]=s3;
    222                 }
    223                 else
    224                 {
    225                     count--;
    226                 }
    227                 count++;
    228             }
    229             for(i=0;i<m1;i++)
    230              {
    231                  switch(a[i][2])
    232                  {
    233                  case 0:{System.out.print(a[i][0]+"+"+a[i][1]+"="+"   ");
    234                  break;}
    235                  case 1:{System.out.print(a[i][0]+"-"+a[i][1]+"="+"   ");
    236                  break;}
    237                  case 2:{System.out.print(a[i][0]+"*"+a[i][1]+"="+"   ");
    238                  break;}
    239                  case 3:{System.out.print(a[i][0]+"/"+a[i][1]+"="+"   ");
    240                  break;}
    241                  }
    242                  n1=s.nextInt();
    243                  if(n1==a[i][0]+a[i][1]||n1==a[i][0]-a[i][1]||n1==a[i][0]*a[i][1]||n1==a[i][0]/a[i][1])
    244                  {
    245                      count1++;
    246                  }
    247                  else
    248                  {
    249                      b[count2][0]=a[i][0];
    250                      b[count2][1]=a[i][1];
    251                      b[count2][2]=a[i][2];
    252                      b[count2][3]=n1;
    253                      count2++;
    254                  }
    255              }
    256             System.out.println("测试结果如下:");
    257             System.out.println("共"+m1+"道题,答对"+count1+"道题,答错"+count2+"道题");
    258             System.out.println("错题如下:");
    259             for(i=0;i<count2;i++)
    260             {
    261                 switch(b[i][2])
    262                  {
    263                  case 0:{System.out.print(b[i][0]+"+"+b[i][1]+"="+b[i][3]+"   ");
    264                  break;}
    265                  case 1:{System.out.print(b[i][0]+"-"+b[i][1]+"="+b[i][3]+"   ");
    266                  break;}
    267                  case 2:{System.out.print(b[i][0]+"*"+b[i][1]+"="+b[i][3]+"   ");
    268                  break;}
    269                  case 3:{System.out.print(b[i][0]+"/"+b[i][1]+"="+b[i][3]+"   ");
    270                  break;}
    271                  }
    272             }
    273 
    274         }
    275         else
    276         {
    277             System.out.println("请选测数值范围:");
    278             int a1=s.nextInt();
    279             int a2=s.nextInt();
    280             int count=0;
    281             while(count<m1)
    282             {
    283                 boolean m=true;
    284                 int s1=(r.nextInt(a2-a1+1)+a1);
    285                 int s2=(r.nextInt(a2-a1+1)+a1);
    286                 int s3=(r.nextInt(ch.length));
    287                 for(i=0;i<=count;i++)
    288                 {
    289                     if(a[i][0]==s1&&a[i][1]==s2&&a[i][2]==s3)
    290                     {
    291                         m=false;
    292                     }
    293                 }
    294                 if(m=true)
    295                 {
    296                     a[count][0]=s1;
    297                     a[count][1]=s2;
    298                     a[count][2]=s3;
    299                 }
    300                 else
    301                 {
    302                     count--;
    303                 }
    304                 count++;
    305             }
    306             for(i=0;i<m1;i++)
    307              {
    308                  switch(a[i][2])
    309                  {
    310                  case 0:{System.out.print(a[i][0]+"+"+a[i][1]+"="+"   ");
    311                  break;}
    312                  case 1:{System.out.print(a[i][0]+"-"+a[i][1]+"="+"   ");
    313                  break;}
    314                  case 2:{System.out.print(a[i][0]+"*"+a[i][1]+"="+"   ");
    315                  break;}
    316                  case 3:{System.out.print(a[i][0]+"/"+a[i][1]+"="+"   ");
    317                  break;}
    318                  }
    319                  n1=s.nextInt();
    320                  if(n1==a[i][0]+a[i][1]||n1==a[i][0]-a[i][1]||n1==a[i][0]*a[i][1]||n1==a[i][0]/a[i][1])
    321                  {
    322                      count1++;
    323                  }
    324                  else
    325                  {
    326                      b[count2][0]=a[i][0];
    327                      b[count2][1]=a[i][1];
    328                      b[count2][2]=a[i][2];
    329                      b[count2][3]=n1;
    330                      count2++;
    331                  }
    332              }
    333             System.out.println("测试结果如下:");
    334             System.out.println("共"+m1+"道题,答对"+count1+"道题,答错"+count2+"道题");
    335             System.out.println("错题如下:");
    336             for(i=0;i<count2;i++)
    337             {
    338                 switch(b[i][2])
    339                  {
    340                  case 0:{System.out.print(b[i][0]+"+"+b[i][1]+"="+b[i][3]+"   ");
    341                  break;}
    342                  case 1:{System.out.print(b[i][0]+"-"+b[i][1]+"="+b[i][3]+"   ");
    343                  break;}
    344                  case 2:{System.out.print(b[i][0]+"*"+b[i][1]+"="+b[i][3]+"   ");
    345                  break;}
    346                  case 3:{System.out.print(b[i][0]+"/"+b[i][1]+"="+b[i][3]+"   ");
    347                  break;}
    348                  }
    349             }
    350         }
    351     }
    352 }

    三、运行结果

    四、总结

         通过这次练习将自己程序又进一步的完善,满足用户的基本要求,但还有很多地方需要在日后的学习中去完善;

    比如:除法运算可以精确到小数的位数等等。在今后的编写程序的时候会考虑好问题的每一个细节。

     五、项目计划表

    项目计划总结

    周活动总结表

    姓名:赵玉璇                                                       日期:2015/3/22

    任务

    日期

    听课

    编程

    阅读课外数目

    锻炼身体

    写老师留得作业

    业余活动

    日总计

    (分钟)

    周日

    180

    120

    60

    360

    周一

    60

    60

    30

    60

    120

    330

    周二

    100

    120

    30

    120

    120

    580

    周三

    120

    60

    180

    360

    周四

    90

    90

    30

    60

    120

    390

    周五

    100

    100

    30

    120

    350

    周六

    240

    120

    180

    540

    阶段时间和效率

    周数:1

    总计

    200

    910

    330

    180

    600

    600

    2940

    平均

    30

    120

    50

    30

    90

    90

    400

    最大

    100

    240

    120

    120

    120

    180

    540

    最小

    100

    60

    30

    30

    60

    120

    330

    2,时间记录日志:

    学生:赵玉璇                                                                     日期:2015/3/22

    老师:王建民                                                                      课程:PSP

    日期

    开始时间

    结束时间

    中断时间

    净时间

    活动

    备注

    3/17

    08:00

    12:00

    40

    200

    上课

    14:30

    18:30

    40

    200

    编程

    19:00

    20:00

    10

    120

    写作业

    22:00

    22:30

    30

    读大道至简

    3/18

    08:00

    10:00

    20

    100

    听课

    11:00

    12:00

    60

    编程

    14:00

    15:00

    60

    写作业

    21:00

    21:30

    30

    读梦断代码

    3/19

    10:00

    12:00

    20

    100

    听课

    14:00

    14:30

    30

    编程

    15:00

    16:00

    60

    写作业

    21:00

    21:30

    30

    读梦断代码

    3/20

    08:00

    12:00

    40

    200

    听课

    14:00

    15:40

    100

    编程

    21:00

    21:30

    30

    读梦断代码

    3/21

    08:00

    14:30

    18:00

    11:00

    17:00

    20:30

    180

    150

    150

    休息

    学习

    编程

    3,缺陷记录日志:

    学生:赵玉璇                                                                  日期:2015/3/22

    老师:王建民 

    日期

    编号

    类型

    引入阶段

    排除阶段

    修复阶段

    修复缺陷

    3/19

    1

    01

    编写

    编译

    2min

    括号丢失

    2

    3/21

    3

    02

    编写

    编译

    20min

    循环错误,计数有误

    4

    02

    编写

    编译

    10min

    输出有误,语法错误

                                                                     

  • 相关阅读:
    TestNG 入门教程
    Angularjs中文版本开发指南发布
    你是码农 还是优秀程序员?
    java牛人给新人的几点建议
    有一种毒药叫励志书
    牛人眼中如何精通spring?
    驯服你的Windows Server 2003
    windows 7 里面的iis在哪里
    神器 cmder
    亚信UED前端流程自动化构建工具
  • 原文地址:https://www.cnblogs.com/yuji5656/p/4353568.html
Copyright © 2011-2022 走看看