• 四则运算2之小学二年级


    一、解题思路
    1、定义一个加法运算函数“add()”和加减乘除运算函数“mul”;利用选择语句用户可以 根据需求选择四则运算的类型和实现参数控制。
    2、使用随机函数Random()产生运算数及运算符,在while()循环里用户可以定义生成算术题 的个数,将产生的运算数及运算符放入定义好的二维数组内,利用if...else...和循环 语句将生成的新的算式与数组中存入的逐一比较,避免算术题重复。
    3、通过switch语句和if((i+1%m2)==0)语句实现控制打印方式的选择。


    二、源代码

      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;
     30         char[] ch={'+','-',};//字符数组
     31         int a[][]=new int[10000][3];
     32         Random r=new Random();
     33         Scanner s=new Scanner(System.in);
     34         System.out.print("请输入算术题的个数:  ");
     35         int m1=s.nextInt();
     36         System.out.print("请输入每行打印的个数:  ");
     37         int m2=s.nextInt();
     38         System.out.print("加减是否有负数(1/0):");
     39         int c1=s.nextInt();
     40         if(c1==1)
     41         {
     42             System.out.println("请选测数值范围:");
     43             int a1=s.nextInt();
     44             int a2=s.nextInt();
     45             
     46             int count=0;
     47             System.out.println("******算数如下******");
     48             while(count<m1)
     49             {
     50                 boolean m=true;
     51                 int s1=(r.nextInt(a2-a1+1)+a1);
     52                 int s2=(r.nextInt(a2-a1+1)+a1);
     53                 int s3=(r.nextInt(ch.length));
     54                 for(i=0;i<=count;i++)
     55                 {
     56                     if(a[i][0]==s1&&a[i][1]==s2&&a[i][2]==s3)
     57                     {
     58                         m=false;
     59                     }
     60                 }
     61                 if(m=true)
     62                 {
     63                     a[count][0]=s1;
     64                     a[count][1]=s2;
     65                     a[count][2]=s3;
     66                 }
     67                 else
     68                 {
     69                     count--;
     70                 }
     71                 count++;
     72             }
     73             //System.out.println("******算数如下******");
     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                  }
     84                  if((i+1)%m2==0)
     85                  {
     86                      System.out.println();
     87                  }
     88              }
     89 
     90         }
     91         else
     92         {
     93             System.out.println("请选测数值范围:");
     94             int a1=s.nextInt();
     95             int a2=s.nextInt();
     96             int count=0;
     97             while(count<m1)
     98             {
     99                 boolean m=true;
    100                 int s1=(r.nextInt(a2-a1+1)+a1);
    101                 int s2=(r.nextInt(a2-a1+1)+a1);
    102                 int s3=(r.nextInt(ch.length));
    103                 for(i=0;i<=count;i++)
    104                 {
    105                     if(a[i][0]==s1&&a[i][1]==s2&&a[i][2]==s3)
    106                     {
    107                         m=false;
    108                     }
    109                 }
    110                 if(m=true)
    111                 {
    112                     a[count][0]=s1;
    113                     a[count][1]=s2;
    114                     a[count][2]=s3;
    115                 }
    116                 else
    117                 {
    118                     count--;
    119                 }
    120                 count++;
    121             }
    122             for(i=0;i<m1;i++)
    123              {
    124                  switch(a[i][2])
    125                  {
    126                  case 0:{System.out.print(a[i][0]+"+"+a[i][1]+"="+"   ");
    127                  break;}
    128                  case 1:{System.out.print(a[i][0]+"-"+a[i][1]+"="+"   ");
    129                  break;}
    130                  }
    131                  if((i+1)%m2==0)
    132                  {
    133                      System.out.println();
    134                  }
    135 
    136              }
    137         }
    138     }
    139     
    140     public static void mul()
    141     {              //乘除法
    142         System.out.println("******乘除法运算******");
    143         int i;
    144         Random r=new Random();
    145         Scanner s=new Scanner(System.in);
    146         System.out.print("请输入算术题的个数:  ");
    147         int m1=s.nextInt();
    148         System.out.print("请输入每行打印的个数:  ");
    149         int m3=s.nextInt();
    150         System.out.print("加减是否有负数(0/1):");
    151         int c2=s.nextInt();
    152         if(c2==1)
    153         {
    154             System.out.println("请选测数值范围:");
    155             int a1=s.nextInt();
    156             int a2=s.nextInt();
    157             char[] ch={'+','-','*','/'};//字符数组
    158             int a[][]=new int[100][3];
    159             int count=0;
    160             while(count<m1)
    161             {
    162                 boolean m=true;
    163                 int s1=(r.nextInt(a2-a1+1)+a1);
    164                 int s2=(r.nextInt(a2-a1+1)+a1);
    165                 int s3=(r.nextInt(ch.length));
    166                 for(i=0;i<=count;i++)
    167                 {
    168                     if(a[i][0]==s1&&a[i][1]==s2&&a[i][2]==s3)
    169                     {
    170                         m=false;
    171                     }
    172                 }
    173                 if(m=true)
    174                 {
    175                     a[count][0]=s1;
    176                     a[count][1]=s2;
    177                     a[count][2]=s3;
    178                 }
    179                 else
    180                 {
    181                     count--;
    182                 }
    183                 count++;
    184             }
    185             for(i=0;i<m1;i++)
    186              {
    187                  switch(a[i][2])
    188                  {
    189                  case 0:{System.out.print(a[i][0]+"+"+a[i][2]+"="+"   ");
    190                      break;}
    191                  case 1:{System.out.print(a[i][0]+"-"+a[i][2]+"="+"   ");
    192                  break;}
    193                  case 2:{System.out.print(a[i][0]+"*"+a[i][2]+"="+"   ");
    194                  break;}
    195                  case 3:{System.out.print(a[i][0]+"/"+a[i][2]+"="+"   ");
    196                  break;}
    197                  }
    198                  if((i+1)%m3==0)
    199                  {
    200                      System.out.println();
    201                  }
    202              }
    203 
    204         }
    205         else
    206         {
    207             System.out.println("请选测数值范围:");
    208             int a1=s.nextInt();
    209             int a2=s.nextInt();
    210             char[] ch={'+','-','*','/'};//字符数组
    211             int a[][]=new int[10000][3];
    212             int count=0;
    213             while(count<m1)
    214             {
    215                 boolean m=true;
    216                 int s1=(r.nextInt(a2-a1+1)+a1);
    217                 int s2=(r.nextInt(a2-a1+1)+a1);
    218                 int s3=(r.nextInt(ch.length));
    219                 for(i=0;i<=count;i++)
    220                 {
    221                     if(a[i][0]==s1&&a[i][1]==s2&&a[i][2]==s3)
    222                     {
    223                         m=false;
    224                     }
    225                 }
    226                 if(m=true)
    227                 {
    228                     a[count][0]=s1;
    229                     a[count][1]=s2;
    230                     a[count][2]=s3;
    231                 }
    232                 else
    233                 {
    234                     count--;
    235                 }
    236                 count++;
    237             }
    238             for(i=0;i<m1;i++)
    239              {
    240                  switch(a[i][2])
    241                  {
    242                  case 0:{System.out.print(a[i][0]+"+"+a[i][2]+"="+"   ");
    243                  break;}
    244                  case 1:{System.out.print(a[i][0]+"-"+a[i][2]+"="+"   ");
    245                  break;}
    246                  case 2:{System.out.print(a[i][0]+"*"+a[i][2]+"="+"   ");
    247                  break;}
    248                  case 3:{System.out.print(a[i][0]+"/"+a[i][2]+"="+"   ");
    249                  break;}
    250                  }
    251                  if((i+1)%m3==0)
    252                  {
    253                      System.out.println();
    254                  }
    255              }
    256         }
    257     }
    258 }

     

    三、运行结果

    四、反思总结
    1、在编写过程中,对于题目避免重复的要求,设想了多种方法实现,最终采用了存入 数组逐一比较的方法实现,思考过程用了很长时间。说明平时锻炼比较少,对于循 环、算法的使用还不够熟悉,以后在这方面应该多加练习。
    2、编程中出现了一些小错误,分号使用、单引号和双引号的区别混淆,说明对于Java 这门语言还不够熟悉,需要多看多练。

    五、日志记录

  • 相关阅读:
    SPI传输协议笔记
    Linux power supply class
    linux ramdisk 参数问题
    Android事件处理过程分析
    PWM 参数计算
    6.828 lab3
    6.828 lab1
    i.MX53 上电启动过程
    linux jiffies的比较
    在arm板上安装Debian
  • 原文地址:https://www.cnblogs.com/yuji5656/p/4339087.html
Copyright © 2020-2023  润新知