首页 > 其他分享 >四则运算课堂测试(三)

四则运算课堂测试(三)

时间:2022-10-04 22:11:31浏览次数:60  
标签:str int 四则运算 number ++ num 测试 answer 课堂

阶段1: 1、定义主界面菜单, 1、参数设置 2、小学二年级口算题 3、小学三年级口算题 4、小学四年级口算题 5、错题集 其中参数设置包括出题个数(缺省值为30)、操作数数值范围。 小学二年级口算题操作数为两个、可进行加减乘除运算(除法必须可以整除),操作数范围不超过100. 小学三年级口算题操作数不超过4个,可以进行加减乘除,操作数范围不超过1000.(要求采用继承小学二年级出题类的方式,实现小学三年级的出题类) 小学四年级口算题操作数不超过5个,可以进行加减乘除,还可以加入括号运算。 (要求采用继承小学三年级出题类的方式,实现小学四年级的出题类)

阶段2: 1、学生实时答题结束后,可以选择是否进行下一套题目答题,如果选择是,则抽取下一套题进行答题,答题结束可以通过查看错题本,查询今日做题正确率。 2、也可以针对错题进行二次答题。

(一)主函数,菜单实现

 1 package Math_06;
 2 
 3 import java.util.Scanner;
 4 
 5 public class Main {
 6     static int num;
 7     static int fw;
 8     static Scanner sc  = new Scanner(System.in);
 9     public static void main (String [] args)
10     {
11 
12         while(true) {
13             System.out.println("1、参数设置");
14             System.out.println("2、小学二年级口算题");
15             System.out.println("3、小学三年级口算题");
16             System.out.println("4、小学四年级口算题");
17             System.out.println("5、错题集");
18 
19             int str = sc.nextInt();
20             switch(str) {
21             case 1 -> Setnumber();
22             case 2 -> student_02( );
23             case 3 -> student_03();
24             case 4 -> student_04();
25             case 5 -> Wrong();
26             default -> System.out.println("输入错误");
27             }
28         }
29     }
30         public static void Setnumber()
31         {
32             System.out.println("请输入出题个数");
33             num = sc.nextInt();
34             System.out.println("请输入出题范围");
35             fw = sc.nextInt();
36         }
37         public static void student_02()
38         {
39             Student_02 s = new Student_02(num , fw);
40             s.init(num, fw);
41             s.show( num);
42             
43             System.out.println("是否进入错题集 : 是1     否2");
44             int j= sc.nextInt();
45             
46             if(j == 1)
47             {
48                 s.showben();
49             }
50         }
51         public static void student_03()
52         {
53             Student_03 s = new Student_03(num , fw);
54             s.init(num, fw);
55             s.show_rate( );
56             
57             System.out.println("是否进入错题集 : 是1     否2");
58             int j= sc.nextInt();
59             if(j == 1)
60             {
61                 s.showben();
62             }
63         }
64         public static void student_04()
65         {
66             Student_04 s = new Student_04(num , fw);
67             s.init(num , fw);        
68             System.out.println("是否进入错题集 : 是1     否2");
69             int j= sc.nextInt();
70             
71             if(j == 1)
72             {
73                 s.showben();
74             }
75         }
76         public static void Wrong()
77         {
78             
79         }
80 }

(二)二年级 : 两个操作数

  1 package Math_06;
  2 
  3 import java.util.Random;
  4 import java.util.Scanner;
  5 
  6 public class Student_02 {
  7 
  8     private int num = 30;
  9     private int fw;
 10 
 11     public char[] f = new char[100010];
 12     public int [] s1 = new int [100010];
 13     public int [] s2 = new int [100010];
 14     public int [] answer = new int [100010];
 15     public int [] ben = new int [100010];
 16     public int y = 0;
 17     public int s = 1;
 18      Student_02()
 19     {
 20         
 21     }
 22      Student_02(int num , int fw)
 23     {
 24         this.setNum(num);
 25         this.setFw(fw);
 26     }
 27 
 28      public void init(int num , int fw) {
 29          int t = 0;
 30          Random rm=new Random();  //随机数
 31 
 32          for(int i = 0 ; i < num ; i ++)
 33          {
 34              t++;
 35              //符号
 36              int number_3;
 37              number_3 = rm.nextInt(4)+1;
 38              //数字
 39              int number_1 = rm.nextInt(fw);
 40              int number_2 = rm.nextInt(fw);
 41              s1[t] = number_1 ; 
 42              s2[t] = number_2 ; 
 43              if(number_3==1)
 44              {
 45                  f[t] = '+';
 46                  answer[s++] = number_1 + number_2;
 47              }
 48              else if(number_3==2)
 49              {
 50                  f[t] = '-';
 51                  answer[s++] = number_1 - number_2;
 52              }
 53              else if (number_3==3)
 54              {
 55                  f[t] = '*';
 56                  answer[s++] = number_1 * number_2;
 57              }
 58              else if(number_3==4)
 59              {
 60                  f[t] = '/';
 61                  answer[s++] = number_1 / number_2;
 62              }     
 63          }
 64      }
 65   public void show( int n) {
 66           for(int i = 1 ; i <= n ; i ++) {
 67             System.out.print(s1[i]);
 68 
 69             if(f[i] == '+')
 70             {
 71                 System.out.print("+"); 
 72             }
 73             else if(f[i] == '-')
 74             {
 75 
 76                 System.out.print( "-" );
 77             }
 78             else if (f[i] == '*')
 79             {
 80 
 81                 System.out.print( "*" );
 82             }
 83             else if(f[i] == '/')
 84             {
 85 
 86                 System.out.print( "/" );
 87             }    
 88 
 89             System.out.print(s2[i]);
 90             System.out.println( "=\t\t" );    
 91             Scanner sc = new Scanner(System.in);
 92             
 93               int d = sc.nextInt();
 94               if(d != answer[i])
 95               {
 96                     System.out.println( "答案错误" );
 97                     ben[y++] = i;
 98               }
 99  
100           }
101              System.out.println("错题个数: " + y);
102                 int b = (n-y)*10/n*10;
103                 int d = b +  (n-y)*10%n*10/n;
104                 System.out.println("正确率: " + d + "%" );
105     }
106   public void showben()
107   {
108       System.out.println("错题本重练系统");
109       for(int i = 0 ; i < y ; i ++) {
110           System.out.print(s1[ben[i]]);
111 
112           if(f[ben[i]] == '+')
113           {
114               System.out.print("+"); 
115           }
116           else if(f[ben[i]] == '-')
117           {
118 
119               System.out.print( "-" );
120           }
121           else if (f[ben[i]] == '*')
122           {
123 
124               System.out.print( "*" );
125           }
126           else if(f[ben[i]] == '/')
127           {
128 
129               System.out.print( "/" );
130           }    
131 
132           System.out.print(s2[ben[i]]);
133           System.out.println( "=\t\t" );    
134           Scanner sc = new Scanner(System.in);
135 
136           int d = sc.nextInt();
137           if(d != answer[ben[i]])
138           {
139               System.out.println( "答案错误" );
140           }
141       }
142   }
143   public int getNum() {
144       return num;
145   }
146   public void setNum(int num) {
147       this.num = num;
148   }
149   public int getFw() {
150       return fw;
151   }
152   public void setFw(int fw) {
153       this.fw = fw;
154   }
155 
156 }

(三)三年级 : 实现4个操作数 (难点 : 4个数的算数优先级运算)

  1 package Math_06;
  2 
  3 import java.util.Random;
  4 import java.util.Scanner;
  5 
  6 public class Student_03 extends Student_02{
  7     public int [] answer_1 = new int [100010];
  8     static String []arr = new String[100000];
  9     int s = 1;
 10     int t = 1;
 11 
 12      Student_03(int num , int fw)
 13     {
 14         super(num , fw);        
 15     }
 16     @Override
 17     public void init(int num , int fw) {
 18 
 19         Random rm=new Random();  //随机数
 20 
 21         for(int i = 0 ; i < num ; i ++)
 22         {
 23             int num_f = 1;
 24             int num_s = 1;
 25             //前两个数字 +-*/   加减不算,乘除算
 26             int number_1 = rm.nextInt(fw);
 27             int number_2 = rm.nextInt(fw);
 28             int number_3;
 29             number_3 = rm.nextInt(4)+1;
 30 
 31             String str = "" + number_1;
 32 
 33             if(number_3 == 3)
 34             {
 35                 answer[num_s ] =  number_1 * number_2;
 36                 str += '*';
 37             }
 38             else if(number_3 == 4)
 39             {
 40                 answer[num_s ] =  number_1 / number_2;
 41                 str += '/';
 42             }
 43             else 
 44             {
 45                 answer[num_s++] = number_1;
 46                 answer[num_s ] = number_2;
 47                 if(number_3 == 1)
 48                 {
 49                     f[num_f++] = '+';
 50                     str += '+';
 51                 }
 52                 else 
 53                 {
 54                     f[num_f++] = '-';
 55                     str += '-';
 56                 }
 57             }
 58             str +=  number_2;
 59             //后两个数 , 加减不算,乘除算
 60             for(int j = 0 ; j < 2; j++)
 61             {
 62                 number_3 = rm.nextInt(4)+1;
 63                 int number_4 = rm.nextInt(fw);
 64 
 65                 if(number_3 == 3)
 66                 {
 67                     answer[num_s ] *= number_4;
 68                     str += '*';
 69                 }
 70                 else if(number_3 == 4)
 71                 {
 72                     answer[num_s ] /= number_4;
 73                     str += '/';
 74                 }
 75                 else 
 76                 {
 77                     answer[++num_s] = number_4;
 78                     if(number_3 == 1)
 79                     {
 80                         f[num_f++] = '+';
 81                         str += '+';
 82                     }
 83                     else 
 84                     {
 85                         f[num_f++] = '-';
 86                         str += '-';
 87                     }
 88                 }
 89                 str +=  number_4;     
 90             }
 91             
 92         //乘除已算完 , 只剩加减 , 计算最终答案
 93             answer_1[s] = answer[1];
 94             for(int j = 1 ;j < num_f ; j++)
 95             {
 96                 if(f[j] == '+')
 97                     answer_1[s] += answer[j+1];
 98                 else 
 99                     answer_1[s] -= answer[j+1];
100             }
101             show(str , answer_1[s]);
102             s++;
103         }
104     }
105     public void show(String str , int  answer ) {
106 
107         arr[t] = str;
108 
109         System.out.println( arr[t] +"=\t\t" );    
110         Scanner sc = new Scanner(System.in);
111 
112         int d = sc.nextInt();
113         if(d != answer )
114         {
115             System.out.println( "答案错误" );
116             ben[y++] = t;
117         }
118         t++;
119     }
120     public void show_rate()
121     {
122         System.out.println("错题个数: " + y);
123         int b = (getNum()-y)*10/getNum()*10;
124         int d1 = b +  (getNum()-y)*10%getNum()*10/getNum();
125         System.out.println("正确率: " + d1 + "%" );
126     }
127     @Override
128     public void showben()
129     {
130          System.out.println("错题本重练系统");
131           for(int i = 0 ; i < y ; i ++) {
132 
133                 System.out.println( arr[ ben[i] ] + "=\t\t" );    
134                 Scanner sc = new Scanner(System.in);
135 
136                 int d = sc.nextInt();
137                 if(d != answer_1[ ben[i] ] )
138                 {
139                     System.out.println( "答案错误" );
140                 }
141           }
142     }
143 
144 }

(四)四年级 : 加括号运算 (难点 : 加入括号后的优先级运算)

  1 package Math_06;
  2 
  3 import java.util.Random;
  4 import java.util.Scanner;
  5 
  6 public class Student_04 extends Student_03{
  7 
  8      Student_04(int num , int fw)
  9     {
 10         super(num , fw);        
 11     }
 12      @Override
 13      public void init(int num , int fw) {
 14          
 15          Random rm=new Random();  //随机数
 16 
 17          for(int i = 0 ; i < num ; i ++)
 18          {
 19              int cnt = 0;
 20              int flag = 0;
 21                 int num_f = 1;
 22                 int num_s = 1;
 23              //前两个数字 +-*/   加减不算,乘除算
 24              int number_1 = rm.nextInt(fw)+1;
 25              int number_2 = rm.nextInt(fw)+1;
 26              int number_3 = rm.nextInt(4)+1;
 27              String str = "";
 28 
 29              //前括号
 30              if(number_1 % 2 == 0 || number_1 % 3 == 0)
 31              {
 32                  cnt = 1;
 33                  str = "(" + number_1;
 34              }
 35              if(cnt == 1) //有括号
 36              {
 37                  if(number_3==1)
 38                  {
 39                      str += '+';
 40                      answer[num_s ] = number_1 + number_2;
 41                  }
 42                  else if(number_3==2)
 43                  {
 44                      str += '-';
 45                      answer[num_s ] = number_1 - number_2;
 46                  }
 47                  else if (number_3==3)
 48                  {
 49                      str += '*';
 50                      answer[num_s ] = number_1 * number_2;
 51                  }
 52                  else if(number_3==4)
 53                  {
 54                      str += '/';
 55                      answer[num_s ] = number_1 / number_2;
 56                  }     
 57                  str +=  number_2 + ")";
 58              }
 59              //没括号
 60              else {
 61                  str += number_1 ;
 62                  if(number_3 == 3)
 63                  {
 64                      answer[num_s ] =  number_1 * number_2;
 65                      str += '*';
 66                  }
 67                  else if(number_3 == 4)
 68                  {
 69                      answer[num_s ] =  number_1 / number_2;
 70                      str += '/';
 71                  }
 72                  else 
 73                  {
 74                      answer[num_s++] = number_1;
 75                      answer[num_s ] = number_2;
 76                      if(number_3 == 1)
 77                      {
 78                          f[num_f++] = '+';
 79                          str += '+';
 80                      }
 81                      else 
 82                      {
 83                          f[num_f++] = '-';
 84                          str += '-';
 85                      }
 86                  }
 87                  str +=  number_2;
 88              }
 89 
 90              //后括号
 91              if( cnt == 0)
 92              {
 93                  flag = 1;
 94              }
 95              //有括号
 96              if(flag == 1)
 97              {
 98                  int number_4 = rm.nextInt(4)+1;
 99                  number_1 = rm.nextInt(fw);
100                  number_2 = rm.nextInt(fw);
101                  number_3 = rm.nextInt(4)+1;
102                  int fu = 0;
103                  if(number_3==1)
104                  {
105                      fu = 1;
106                      answer[++num_s ] = number_1 + number_2;
107                  }
108                  else if(number_3==2)
109                  {
110                      fu = 2;
111                      answer[++num_s ] = number_1 - number_2;
112                  }
113                  else if (number_3==3)
114                  {
115                      fu = 3;
116                      answer[++num_s ] = number_1 * number_2;
117                  }
118                  else if(number_3==4)
119                  {
120                      fu = 0;
121                      answer[++num_s ] = number_1 / number_2;
122                  }     
123                 
124             //计算2,3连接处
125                  if(number_4 == 3)
126                  {
127                      answer[--num_s ] *=  answer[++num_s ];
128                      str += '*';
129                  }
130                  else if(number_4 == 4)
131                  {
132                      answer[--num_s ] /=  answer[++num_s ];
133                      str += '/';
134                  }
135                  else 
136                  {   
137                      if(number_4 == 1)
138                      {
139                          f[num_f++] = '+';
140                          str += '+';
141                      }
142                      else 
143                      {
144                          f[num_f++] = '-';
145                          str += '-';
146                      }
147                  }
148                  str += "(" + number_1;
149                  if(fu == 1)
150                      str += '+';
151                  else if (fu == 2)
152                      str += '-';
153                  else if(fu == 3)
154                      str += '*';
155                  else str += '/';
156                  str +=  number_2 + ")";
157              }
158              //无括号
159              else {
160                  for(int j = 0 ; j < 2; j++)
161                  {
162                      number_3 = rm.nextInt(4)+1;
163                      int number_4 = rm.nextInt(fw);
164 
165                      if(number_3 == 3)
166                      {
167                          answer[num_s ] *= number_4;
168                          str += '*';
169                      }
170                      else if(number_3 == 4)
171                      {
172                          answer[num_s ] /= number_4;
173                          str += '/';
174                      }
175                      else 
176                      {
177                          answer[++num_s] = number_4;
178                          if(number_3 == 1)
179                          {
180                              f[num_f++] = '+';
181                              str += '+';
182                          }
183                          else 
184                          {
185                              f[num_f++] = '-';
186                              str += '-';
187                          }
188                      }
189                      str +=  number_4;     
190                  }
191              }
192                 
193             //乘除已算完 , 只剩加减 , 计算最终答案
194                 answer_1[s] = answer[1];
195                 for(int j = 1 ;j < num_f ; j++)
196                 {
197                     if(f[j] == '+')
198                         answer_1[s] += answer[j+1];
199                     else 
200                         answer_1[s] -= answer[j+1];
201                 }
202                 show(str , answer_1[s]);
203                 s++;
204             }
205         }
206      public void show(String str , int  answer ) {
207 
208             arr[t] = str;
209 
210             System.out.println( arr[t] +"=\t\t" );    
211             Scanner sc = new Scanner(System.in);
212 
213             int d = sc.nextInt();
214             if(d != answer )
215             {
216                 System.out.println( "答案错误" + answer );
217                 ben[y++] = t;
218             }
219             t++;
220         }
221         public void show_rate()
222         {
223             System.out.println("错题个数: " + y);
224             int b = (getNum()-y)*10/getNum()*10;
225             int d1 = b +  (getNum()-y)*10%getNum()*10/getNum();
226             System.out.println("正确率: " + d1 + "%" );
227         }
228         @Override
229         public void showben()
230         {
231              System.out.println("错题本重练系统");
232               for(int i = 0 ; i < y ; i ++) {
233 
234                     System.out.println( arr[ ben[i] ] + "=\t\t" );    
235                     Scanner sc = new Scanner(System.in);
236 
237                     int d = sc.nextInt();
238                     if(d != answer_1[ ben[i] ] )
239                     {
240                         System.out.println( "答案错误" );
241                     }
242               }
243         }
244 }

 

标签:str,int,四则运算,number,++,num,测试,answer,课堂
From: https://www.cnblogs.com/fan-wang/p/16754613.html

相关文章