Java实现四则运算(四)

 修改后的出题系统

1.添加用户自定义是否出现乘除法,自由选择符号和个数,并且可以自定义操作数的取值范围。

复制代码
  1 /*
  2  *     2、可定制(数量/打印方式):输入大的数量值,测试一下系统是否崩溃,反向查找系统是否优化的余地;
  3     3、定制操作数的个数:
  4     4、定制是否有乘除法
  5     5、定制是否有括号(随机加入)
  6     6 、定制数值范围(确定操作数的取值范围)
  7  */
  8 package random;
  9 import java.util.*;
 10 import java.lang.reflect.Array;
 11 import java.math.*;
 12 
 13 public class Com {
 14     @SuppressWarnings({ "unused", "unchecked" })
 15     public static void main(String[] args) {
 16         /*
 17          * 定义数组存储每一次生成的随机数
 18          * 
 19          */
 20         ArrayList<ComDem> array1 = new ArrayList<ComDem>();
 21         Scanner sc=new Scanner(System.in);
 22         ComDem name1=new ComDem();
 23         /*
 24          * resu1   +
 25          * resu2   -
 26          * resu3   *
 27          * resu4   /
 28          * choice     定义随机产生的计算方法加减乘除
 29          * choices     转换格式  switch
 30          */
 31         Random choice=new Random(1);
 32         int choices;
 33         /*
 34          * 格式化输出 5列6行
 35          * 定义 line
 36          */
 37         int line=0;
 38         /*
 39          * 控制操作数的范围
 40          * max
 41          * min
 42          */
 43         while(true) {
 44             System.out.println("请依次输入操作数的最大值和最小值");
 45             name1.max=sc.nextInt();
 46             name1.min=sc.nextInt();
 47             if(name1.max<=name1.min)
 48             {
 49                 System.out.println("你输入的数据有误,最小值应该小于最大值!请重新输入:");
 50             }
 51             else
 52             {
 53                 break;
 54             }
 55         }
 56         Random rand1 =new Random(8);
 57         Random rand2 =new Random(10);
 58         Random rand3 =new Random(12);
 59             name1.num1=rand1.nextInt(name1.max)+name1.min;
 60             name1.num2=rand2.nextInt(name1.max)+name1.min;
 61             name1.num3=rand3.nextInt(name1.max)+name1.min;
 62         array1.add(name1);
 63         int resu1,resu2,resu3;
 64         double resu4;    
 65         /*
 66          * 用户自定义打印题目数量
 67          * 定义变量 printnum;
 68          * 用户自定义是否有乘除法运算是否有带括号运算;
 69          */
 70         int printnum=0;
 71         boolean isbrackets=false;        //是否有括号
 72         boolean isthree=true;           //三个数还是两个数   true 3个   false  2个
 73         boolean ismutiply=true;          //是否有乘法   true有  false没有
 74         boolean isdivide=true;           //是否有除法  true有   false没有
 75         System.out.println("题目是否要出现乘法:( true有  false没有,默认有)");
 76         ismutiply=sc.nextBoolean();
 77         System.out.println("题目是否要出现除法:( true有  false没有,默认有)");
 78         isdivide=sc.nextBoolean();
 79         System.out.println("请输入你要打印的题目数量:");
 80         printnum=sc.nextInt();
 81         System.out.println("(true 3个数的运算,false 2个数的运算)请输入你的选择");
 82         isthree=sc.nextBoolean();
 83         boolean flag=true;             //查重标识
 84         for(int i=0;i<printnum;i++)
 85         {
 86         /*
 87          * 查重
 88          */
 89             for(int j=0; j<array1.size();j++) {
 90                 if(array1.get(j).num1==name1.num1|| array1.get(j).num2==name1.num2  ||
 91                         array1.get(j).num3==name1.num3)
 92                     
 93                     {
 94                     name1.num1=rand1.nextInt(name1.max)+name1.min;
 95                     name1.num2=rand2.nextInt(name1.max)+name1.min;
 96                     name1.num3=rand3.nextInt(name1.max)+name1.min;
 97                     }
 98                 else
 99                 {
100                     array1.add(name1);
101                     flag=false;
102                 }
103                 
104                 if(flag==false)
105                     break;
106             }
107             /*
108              * 两个数 + - *  /
109              */
110         if(isthree==false  && ismutiply==true && isdivide==true){
111         choices=choice.nextInt(4);
112         switch(choices) {
113         case 0:
114             System.out.print("("+(i+1)+")");
115             resu1=name1.add();
116             break;
117         case 1:
118             System.out.print("("+(i+1)+")");
119             resu2=name1.sub();
120             break;
121         case 2:
122             System.out.print("("+(i+1)+")");
123             resu3=name1.mix();
124             break;
125         case 3:
126             System.out.print("("+(i+1)+")");
127             resu4=name1.chu();
128             break;
129             }
130         }
131         /*
132          * 两个数乘法加减  * + -
133          */
134         if(isthree==false &&ismutiply==true&&isdivide==false){
135             choices=choice.nextInt(3);
136             switch(choices) {
137             case 0:
138                 System.out.print("("+(i+1)+")");
139                 resu1=name1.add();
140                 break;
141             case 1:
142                 System.out.print("("+(i+1)+")");
143                 resu2=name1.sub();
144                 break;
145             case 2:
146                 System.out.print("("+(i+1)+")");
147                 resu3=name1.mix();
148                 break;
149                 }
150             }
151             /*
152              * 两个数除法加减    / + -
153              * 
154              */
155         if(isthree==false & ismutiply==false && isdivide==true){
156             choices=choice.nextInt(3);
157             switch(choices) {
158             case 0:
159                 System.out.print("("+(i+1)+")");
160                 resu1=name1.add();
161                 break;
162             case 1:
163                 System.out.print("("+(i+1)+")");
164                 resu2=name1.sub();
165                 break;
166             case 2:
167                 System.out.print("("+(i+1)+")");
168                 resu4=name1.chu();
169                 break;
170                 }
171             }
172         /*
173          * 两个数加减
174          */
175         if(isthree==false & ismutiply==false && isdivide==false){
176             choices=choice.nextInt(2);
177             switch(choices) {
178             case 0:
179                 System.out.print("("+(i+1)+")");
180                 resu1=name1.add();
181                 break;
182             case 1:
183                 System.out.print("("+(i+1)+")");
184                 resu2=name1.sub();
185                 break;
186                 }
187             }
188         /*
189          * 三个数 + - *  /
190          */
191             if(isthree==true&&ismutiply==true&&isdivide==true) {
192             choices=choice.nextInt(4);
193             switch(choices) {
194             case 0:
195                 System.out.print("("+(i+1)+")");
196                 resu1=name1.thereAdd();
197                 break;
198             case 1:
199                 System.out.print("("+(i+1)+")");
200                 resu2=name1.therrSub();
201                 break;
202             case 2:
203                 System.out.print("("+(i+1)+")");
204                 resu3=name1.threeMix();
205                 break;
206             case 3:
207                 System.out.print("("+(i+1)+")");
208                 resu4=name1.threeChu();
209                 break;
210             }
211         }
212             /*
213              * 三个数 乘法加减
214              */
215             if(isthree==true && ismutiply==true && isdivide==false)
216             {
217                 choices=choice.nextInt(3);
218                 switch(choices) {
219                 case 0:
220                     System.out.print("("+(i+1)+")");
221                     resu1=name1.thereAdd();
222                     break;
223                 case 1:
224                     System.out.print("("+(i+1)+")");
225                     resu2=name1.therrSub();
226                     break;
227                 case 2:
228                     System.out.print("("+(i+1)+")");
229                     resu3=name1.threeMix();
230                     break;
231                 }
232             }
233                 
234                 /*
235                  * 三个数 除法加减
236                  */
237                 if(isthree==true && ismutiply==false && isdivide==true)
238                 {
239                     choices=choice.nextInt(3);
240                     switch(choices) {
241                     case 0:
242                         System.out.print("("+(i+1)+")");
243                         resu1=name1.thereAdd();
244                         break;
245                     case 1:
246                         System.out.print("("+(i+1)+")");
247                         resu2=name1.therrSub();
248                         break;
249                     case 2:
250                         System.out.print("("+(i+1)+")");
251                         resu4=name1.threeChu();
252                         break;
253                     }
254                 }
255                     /*
256                      * 三个数 + - 只有加减
257                      * 
258                      */
259                     if(isthree==true && ismutiply==false && isdivide==false)
260                     {
261                         choices=choice.nextInt(2);
262                         switch(choices) {
263                         case 0:
264                             System.out.print("("+(i+1)+")");
265                             resu1=name1.thereAdd();
266                             break;
267                         case 1:
268                             System.out.print("("+(i+1)+")");
269                             resu2=name1.therrSub();
270                             break;
271                         }
272                     }
273                     
274             /*
275              * 控制5个一换行
276              */
277             line++;
278             if(line>4 && line%5 ==0)
279             {
280                 System.out.println();
281             }
282             /*
283              * 调用随机数函数;
284              */
285             name1.num1=rand1.nextInt(101);
286             name1.num2=rand2.nextInt(101);
287             name1.num3=rand3.nextInt(101);
288             sc.close();
289         }
290     }
291 }
292
复制代码
 1 package random;
 2 import java.util.*;
 3 import java.math.*;
 4 public class ComDem {
 5     /*
 6      * 定义几个操作数 完成运算
 7      * 
 8      */
 9     public int  num1;
10     public int num2;
11     public int num3;
12     public int max;
13     public int min;
14     public int getNum3() {
15         return num3;
16     }
17     public void setNum3(int num3) {
18         this.num3 = num3;
19     }
20     public double getNum1() {
21         return num1;
22     }
23     public void setNum1(int num1) {
24         this.num1 = num1;
25     }
26     public double getNum2() {
27         return num2;
28     }
29     public void setNum2(int num2) {
30         this.num2 = num2;
31     }
32     public int getR1() {
33         num1=(int)(Math.random()*100);
34         return num1;
35     }
36     public int getR2() {
37         num2=(int)(Math.random()*100);
38         return num2;
39     }
40     
41     /*
42      * 2 数运算
43      */
44     public int add() {
45         System.out.print(num1+"+"+num2+"="+"\t");
46         return num1+num2;
47     }
48     public int sub() {
49         System.out.print(num1+"-"+num2+"="+"\t");
50         return num1-num2;
51     }
52     public int mix() {
53         System.out.print(num1+"*"+num2+"="+"\t");
54         return num1*num2;
55     }
56     public double chu() {
57         System.out.print(num1+"/"+num2+"="+"\t");
58         return (double)(num1/num2);
59     }
60     /*
61      * 下面定义四个函数   分别是 + - *  / 三个操作数的运算
62      * 
63      */
64     public int thereAdd() {
65         System.out.print(num1+ "+" +  num2 + "+" + num3 +"="+"\t");
66         return num1+num2+num3;
67     }
68     public int therrSub() {
69         System.out.print(num1+"-"+num2+"-" +num3+ "="+"\t");
70         
71         return num1-(num2-num3);
72     }
73     public int threeMix() {
74         System.out.print(num1+"*"+num2+ "*"+num3+"="+"\t");
75         return num1*num2*num3;
76     }
77     public double threeChu() {
78         System.out.print(num1+"/"+num2+"/"+num3+"="+"\t");
79         return (num1/num2)/num3;
80     }
81     public ComDem(int num1, int num2,int num3){
82         this.num1 = num1;
83         this.num2 = num2;
84         this.num3 = num3;
85     }
86     ComDem(){};
87 }
复制代码

 

复制代码

 

posted @   靠谱杨  阅读(167)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具

喜欢请打赏

扫描二维码打赏

了解更多

点击右上角即可分享
微信分享提示