Java实现四则运算(二)

 1 package random;
 2 import java.util.*;
 3 import java.math.*;
 4 public class Com {
 5 
 6     public static void main(String[] args) {
 7         // TODO 自动生成的方法存根
 8         int flag=(int)(Math.random()*100);
 9         ComDem name1=new ComDem();
10         /*
11          * resu1   +
12          * resu2   -
13          * resu3   *
14          * resu4   /
15          */
16         Random choice=new Random(1);
17         int choices;
18         /*
19          * 格式化输出 5列6行
20          * 定义 line  row 
21          */
22         int line=0;
23         Random rand1 =new Random(8);
24         Random rand2 =new Random(10);
25         name1.num1=rand1.nextInt(101);
26         name1.num2=rand2.nextInt(101);
27         int resu1,resu2,resu3;
28         double resu4;    
29         for(int i=0;i<30;i++)
30         {
31         /*
32          * 查重
33          */
34         while(name1.num1==name1.num2)
35         {
36             name1.num1=rand1.nextInt(101);
37             name1.num2=rand2.nextInt(101);
38         }
39         choices=choice.nextInt(4);
40         
41         switch(choices) {
42         case 0:
43             System.out.print("("+(i+1)+")");
44             resu1=name1.add();
45             break;
46         case 1:
47             System.out.print("("+(i+1)+")");
48             resu2=name1.sub();
49             break;
50         case 2:
51             System.out.print("("+(i+1)+")");
52             resu3=name1.mix();
53             break;
54         case 3:
55             System.out.print("("+(i+1)+")");
56             resu4=name1.chu();
57             
58             break;
59         }
60         line++;
61         if(line!=0&&line%5==0)
62         {
63             System.out.println();
64         }
65         name1.num1=rand1.nextInt(101);
66         name1.num2=rand2.nextInt(101);
67         }
68     }
69 }
 1 package random;
 2 import java.util.*;
 3 import java.math.*;
 4 public class ComDem {
 5     public int  num1;
 6     public int num2;
 7     public double getNum1() {
 8         return num1;
 9     }
10     public void setNum1(int num1) {
11         this.num1 = num1;
12     }
13     public double getNum2() {
14         return num2;
15     }
16     public void setNum2(int num2) {
17         this.num2 = num2;
18     }
19     public int getR1() {
20         num1=(int)(Math.random()*100);
21         return num1;
22     }
23     public int getR2() {
24         num2=(int)(Math.random()*100);
25         return num2;
26     }
27     public int add() {
28         System.out.print(num1+"+"+num2+"="+"\t");
29         return num1+num2;
30     }
31     public int sub() {
32         System.out.print(num1+"-"+num2+"="+"\t");
33         return num1-num2;
34     }
35     public int mix() {
36         System.out.print(num1+"*"+num2+"="+"\t");
37         return num1*num2;
38     }
39     public double chu() {
40         System.out.print(num1+"/"+num2+"="+"\t");
41         return (double)(num1/num2);
42     }
43     public ComDem(int num1, int num2){
44         this.num1 = num1;
45         this.num2 = num2;
46     }
47     ComDem(){};
48     
49     
50 }

 

posted @ 2020-10-02 20:28  靠谱杨  阅读(135)  评论(0)    收藏  举报