Java实现四则运算(三)

______________________________________________

学习内容:

改进了自动生成题目程序,用户可以自定义题目的数量,可以自定义题目有几个数参与运算!(并且题目不会出现重复的数字)

缺陷:无法定义两个数和三个数分开出题,不可以定义是否出现带括号运算,不可以定义运算方式!

复制代码
  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         
 22         ComDem name1=new ComDem();
 23         /*
 24          * resu1   +
 25          * resu2   -
 26          * resu3   *
 27          * resu4   /
 28          * choice     定义随机产生的计算方法加减乘除
 29          * choices     转换格式  switch
 30          */
 31         
 32         Random choice=new Random(1);
 33         int choices;
 34         /*
 35          * 格式化输出 5列6行
 36          * 定义 line
 37          */
 38         int line=0;
 39         Random rand1 =new Random(8);
 40         Random rand2 =new Random(10);
 41         Random rand3 =new Random(12);
 42         name1.num1=rand1.nextInt(101);
 43         name1.num2=rand2.nextInt(101);
 44         name1.num3=rand3.nextInt(101);
 45         array1.add(name1);
 46         
 47         int resu1,resu2,resu3;
 48         double resu4;    
 49         /*
 50          * 用户自定义打印题目数量
 51          * 定义变量 printnum;
 52          * 用户自定义是否有乘除法运算是否有带括号运算;
 53          */
 54         int printnum=0;
 55         boolean isbrackets=false;        //是否有括号
 56         boolean isthree=true;           //三个数还是两个数   true 3个   false  2个
 57         Scanner sc=new Scanner(System.in);
 58         System.out.println("请输入你要打印的题目数量:");
 59         printnum=sc.nextInt();
 60         System.out.println("(true 3个数的运算,false 2个数的运算)请输入你的选择");
 61         isthree=sc.nextBoolean();
 62         boolean flag=true;
 63         for(int i=0;i<printnum;i++)
 64         {
 65         /*
 66          * 查重
 67          */
 68             for(int j=0; j<array1.size();j++) {
 69                 if(array1.get(j).num1==name1.num1||  array1.get(j).num2==name1.num2  ||array1.get(j).num3==name1.num3)
 70                     {
 71                         name1.num1=rand1.nextInt(101);
 72                         name1.num2=rand2.nextInt(101);
 73                         name1.num3=rand3.nextInt(101);
 74                     }
 75                 else
 76                 {
 77                     array1.add(name1);
 78                     flag=false;
 79                 }
 80                 if(flag==false)
 81                     break;
 82             }
 83         if(isthree==false){
 84         choices=choice.nextInt(4);
 85         switch(choices) {
 86         case 0:
 87             System.out.print("("+(i+1)+")");
 88             resu1=name1.add();
 89             break;
 90         case 1:
 91             System.out.print("("+(i+1)+")");
 92             resu2=name1.sub();
 93             break;
 94         case 2:
 95             System.out.print("("+(i+1)+")");
 96             resu3=name1.mix();
 97             break;
 98         case 3:
 99             System.out.print("("+(i+1)+")");
100             resu4=name1.chu();
101             break;
102         }
103     }
104             if(isthree==true) {
105             choices=choice.nextInt(4);
106             switch(choices) {
107             case 0:
108                 System.out.print("("+(i+1)+")");
109                 resu1=name1.thereAdd();
110                 break;
111             case 1:
112                 System.out.print("("+(i+1)+")");
113                 resu2=name1.therrSub();
114                 break;
115             case 2:
116                 System.out.print("("+(i+1)+")");
117                 resu3=name1.threeMix();
118                 break;
119             case 3:
120                 System.out.print("("+(i+1)+")");
121                 resu4=name1.threeChu();
122                 break;
123             }
124         }
125         /*
126          * 控制5个一换行
127          */
128         line++;
129         if(line!=0&&line%5==0)
130         {
131             System.out.println();
132         }
133         /*
134          * 调用随机数函数;
135          */
136         name1.num1=rand1.nextInt(101);
137         name1.num2=rand2.nextInt(101);
138         name1.num3=rand3.nextInt(101);
139         }
140         sc.close();
141     }
142 }
复制代码
复制代码
 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 getNum3() {
13         return num3;
14     }
15     public void setNum3(int num3) {
16         this.num3 = num3;
17     }
18     public double getNum1() {
19         return num1;
20     }
21     public void setNum1(int num1) {
22         this.num1 = num1;
23     }
24     public double getNum2() {
25         return num2;
26     }
27     public void setNum2(int num2) {
28         this.num2 = num2;
29     }
30     public int getR1() {
31         num1=(int)(Math.random()*100);
32         return num1;
33     }
34     public int getR2() {
35         num2=(int)(Math.random()*100);
36         return num2;
37     }
38     
39     /*
40      * 2 数运算
41      */
42     public int add() {
43         System.out.print(num1+"+"+num2+"="+"\t");
44         return num1+num2;
45     }
46     public int sub() {
47         System.out.print(num1+"-"+num2+"="+"\t");
48         return num1-num2;
49     }
50     public int mix() {
51         System.out.print(num1+"*"+num2+"="+"\t");
52         return num1*num2;
53     }
54     public double chu() {
55         System.out.print(num1+"/"+num2+"="+"\t");
56         return (double)(num1/num2);
57     }
58     /*
59      * 下面定义四个函数   分别是 + - *  / 三个操作数的运算
60      * 
61      */
62     public int thereAdd() {
63         System.out.print(num1+ "+" +  num2 + "+" + num3 +"="+"\t");
64         return num1+num2+num3;
65     }
66     public int therrSub() {
67         System.out.print(num1+"-"+num2+"-" +num3+ "="+"\t");
68         
69         return num1-(num2-num3);
70     }
71     public int threeMix() {
72         System.out.print(num1+"*"+num2+ "*"+num3+"="+"\t");
73         return num1*num2*num3;
74     }
75     public double threeChu() {
76         System.out.print(num1+"/"+num2+"/"+num3+"="+"\t");
77         return (num1/num2)/num3;
78     }
79     public ComDem(int num1, int num2,int num3){
80         this.num1 = num1;
81         this.num2 = num2;
82         this.num3 = num3;
83     }
84     ComDem(){};
85     
86     
87 }
复制代码

代码测试结果截图:

 

 

 

 

测试 三个数的运算,最大298可以运行

 

 

 

 测试两个数运算  最大运行 298 个式子

 

 

 

 

posted @   靠谱杨  阅读(18)  评论(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工具

喜欢请打赏

扫描二维码打赏

了解更多

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