课堂测试1、2、3

课堂测试1:

像二柱子那样,花二十分钟写一个能自动生成30道小学四则运算题目的 “软件”

 1 public class Math {
 2      public static void main(String[] args) {
 3          Random rm=new Random();
 4             
 5             for (int i=0;i<30;i++){
 6                 int number_3=rm.nextInt(4) + 1;
 7                 int number_1=rm.nextInt(100);
 8                 int number_2=rm.nextInt(100);
 9                     if(number_3==1){
10                         System.out.println(number_1+"+"+number_2 + "=");
11                     }else if(number_3==2){
12                         System.out.println(number_1+"-"+number_2 + "=");
13                     }else if (number_3==3){
14                         System.out.println(number_1+"*"+number_2 + "=");
15                     }else if(number_3==4){
16                         System.out.println(number_1+"/"+number_2 + "=");
17                     }
18             }
19         }
20 }

课堂测试2: (1)题目避免重复; (2)可定制(数量/打印方式);

 1 import java.util.Random;
 2 import java.util.Scanner;
 3 
 4 public class Math_02 {
 5 
 6      public static int s[] = new int[100000];
 7      public static int f[] = new int[100000];
 8      
 9      public static void main(String[] args) {
10     Scanner sc = new Scanner(System.in);
11     System.out.println("请输入一共输入多少个");
12     int n = sc.nextInt();
13     System.out.println("请输入一行个数");
14     int x=sc.nextInt();
15     
16     Random rm=new Random();
17     for(int i = 0 ; i < n ; i ++)
18     {
19         if(i % x == 0)
20             System.out.print("\n");
21         
22         int number_3=rm.nextInt(4) + 1;
23         int number_1=rm.nextInt(100);
24         int number_2=rm.nextInt(100);
25         
26         
27         s[i] = number_3;
28         f[i] = number_1;
29         for (int j = 0; j < i; j++)
30         {
31            if( s[j] ==  s[i] && f[j] == f[i])
32           {
33             number_3++;
34           }
35         }
36             if(number_3==1){
37                 System.out.print(number_1+"+"+number_2 + "=   ");
38             }else if(number_3==2){
39                 System.out.print(number_1+"-"+number_2 + "=   ");
40             }else if (number_3==3){
41                 System.out.print(number_1+"*"+number_2 + "=   ");
42             }else if(number_3==4){
43                 System.out.print(number_1+"/"+number_2 + "=   ");
44             }
45         
46     }
47     
48 }
49 }

课堂测试3: 2、可定制(数量/打印方式):输入大的数量值,测试一下系统是否崩溃,反向查找系统是否优化的余地; 3、定制操作数的个数: 4、定制是否有乘除法 5、定制是否有括号(随机加入) 6 、定制数值范围(确定操作数的取值范围)

  1 import java.util.Random;
  2 import java.util.Scanner;
  3 
  4 public class Math_03 {
  5          
  6     public static void main(String[] args) {
  7         Scanner sc = new Scanner(System.in);
  8         Random rm=new Random();  //随机数
  9          
 10         System.out.println("请输入一共输入多少个");
 11         int n = sc.nextInt();
 12         System.out.println("请输入一行个数");
 13         int h=sc.nextInt();
 14         System.out.println("请输入操作数的个数");
 15         int cz=sc.nextInt();
 16         System.out.println("请输入范围");
 17         int fw=sc.nextInt();
 18         System.out.println("是否需要括号,输入1为需要,0为不需要");
 19         int kh=sc.nextInt();
 20         System.out.println("是否需要乘除法,输入1为需要,0为不需要");
 21         int cc=sc.nextInt();
 22         sc.close();
 23 
 24         for(int i = 0 ; i < n ; i ++)
 25         {
 26             int cnt = 0;
 27             int flag = 0;
 28             
 29             if(i % h == 0)
 30             {
 31                 System.out.print("\n");
 32             }
 33 
 34             for(int j=0 ; j < cz ; j++)
 35             {
 36                 //符号
 37                 int number_3;
 38                 if(cc == 0)
 39                 {
 40                     number_3=rm.nextInt(2) + 1;
 41                 }
 42                 else
 43                 {
 44                     number_3=rm.nextInt(4) + 1;
 45                 }
 46 
 47                 //数字
 48                 int number_1=rm.nextInt(fw) + 1;
 49 
 50                 //括号
 51                 if(kh == 1)
 52                 {
 53                     if(number_1 % 2 == 0 || number_1 % 3 == 0)
 54                     {
 55                         if(flag == 0 && j < cz-1)
 56                             System.out.print("(");
 57                         cnt = 1;
 58                     }
 59                     
 60                     System.out.print(number_1);
 61                     if(flag == 1)
 62                     {
 63                         System.out.print(")");
 64                         cnt = 0;
 65                     }
 66 
 67                     if(j == cz-1)
 68                         break;
 69 
 70                     if(number_3==1)
 71                     {
 72                         System.out.print("+"); 
 73                     }
 74                     else if(number_3==2)
 75                     {
 76                         System.out.print( "-" );
 77                     }
 78                     else if (number_3==3)
 79                     {
 80                         System.out.print( "*" );
 81                     }
 82                     else if(number_3==4)
 83                     {
 84                         System.out.print( "/" );
 85                     }     
 86                     if(cnt == 1)
 87                         flag = 1;
 88                     else
 89                         flag = 0;
 90                 }
 91                 else
 92                 {
 93                     System.out.print(number_1);
 94 
 95                     if(j == cz-1)
 96                         break;
 97 
 98                     if(number_3==1)
 99                     {
100                         System.out.print("+"); 
101                     }
102                     else if(number_3==2)
103                     {
104                         System.out.print( "-" );
105                     }
106                     else if (number_3==3)
107                     {
108                         System.out.print( "*" );
109                     }
110                     else if(number_3==4)
111                     {
112                         System.out.print( "/" );
113                     }     
114                 }
115             }
116             System.out.print( "=\t\t" );
117 
118         }
119     }
120 }

 

posted @ 2022-09-12 11:09  旺旺大菠萝  阅读(23)  评论(0编辑  收藏  举报