四则运算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 这门语言还不够熟悉,需要多看多练。

五、日志记录

时间: 2024-08-10 04:12:21

四则运算2之小学二年级的相关文章

小学二年级四则运算题

一.题目要求 产生300道小学二年级的四则运算的数学题,减法不能出现负数,除法要整除. 二.编写过程 编写产生30道小学二年级的四则运算的数学题,用rand()产生随机数,和随机的符号,在除法运算中在被除数不为零的情况下利用一组随机数来产生被除数和除数是被除数的整数倍,这样保证了除法的整除,减法要先判断两个随机数的大小,输出的结果为大数减去小数.然后利用for循环30次输出30道题目. 三.程序运行 四.程序代码 #include<stdio.h>#include<stdlib.h>

30道小学二年级四则运算题

思路与体会:用rand函数产生随机数32767/327约等于100.2.排除了除数是0的错误, 但是仍有缺陷:减法会出现不够减结果是负数的情况! 运行结果总出现闪退情况,查阅资料后加了system("pause") 运行环境:vs2013 源代码: #include<iostream> using namespace std; #include<stdio.h> #include<stdlib.h>int rak(){ return rand() /

悲催的二柱子们做小学二年级四则运算题(Javaweb)

1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <body background="C:\Users\26624\Desktop/beijing.jpg"> 7

设计四则运算包含真分数的小学二年级运算

本次我在上次的四则运算的基础上加上了真分数运算,在之前的基础上改变一下运算代码 代码如下: #include<stdio.h> #include<Windows.h> #include<time.h> void main() { int a, b, c, d,i,m,n; float p, q; srand(unsigned( time(NULL))); for (i = 0; i < 30; i++) { a = rand() % 100; b = rand()

小学二年级三十道四则运算题目。

代码: #include <stdio.h>#include <string.h>#include <stdlib.h>#include <time.h> int main(int argc, char **argv){    int a[30],b[30];     int i,c,d;        srand((unsigned int)time(NULL));    for (i = 0; i < 30; i++)    {          

小学二年级三十道四则运算题目-扩展运算,添加指定题目数量和支持真分数运算

代码: #include<stdio.h>  #include<Windows.h>  #include<time.h>  void main()  {      int a, b, c, d,i,m,n;      float p, q;      srand(unsigned( time(NULL)));      for (i = 0; i < 30; i++)      {         a = rand() % 100;         b = ran

关于运用C语言自动生成三十道小学二年级四则运算

#include <stdio.h>#include <stdlib.h>#include <time.h> int main(){ int i = 0; srand((unsigned)time(NULL)); while(i<30) { int a = rand()%100; int b = rand()%100; int j; j = rand()%4; printf("%d", a); switch(j) { case 0: print

用Python开发小学二年级口算自动出题程序

版权声明:本文为博主原创文章,欢迎转载,并请注明出处.联系方式:[email protected] 武汉光谷一小二年级要求家长每天要给小孩出口算题目,让孩子练习. 根据老师出题要求编写了Python程序自动出题,结果保存为txt文件,打印出来作为练习用,这样就不用每天繁琐地人工出题了,其中的数字用randint随机产生. 程序如下: # -*- coding:utf-8 -*- __author__ = 'zhengbiqing [email protected]' __doc__ = "&qu

小学二年级题目的改进

以下程序满足的条件: 1.避免重复 随机算取一组随机数,将第二道题和第一道题操作数进行对比,如果发现两个操作数都完全一样话,就删除此组随机数,重新进行对边,直至最后没有重复的数组. 2.可定制 可控制每行打印题目的个数,以及所有算术题的个数从而控制打印格式 3.是否有乘除法 题目随机出现后,将所有运算符号送入一个数组,然后进行检索,查询是否有乘除号 4.数值范围 随机数的取值范围在0-99之间,这样结果就不会超过200. 5.有无括号 当有乘除法的时候有括号,无乘除法的话,就没有括号 6.打印中