二柱子
课堂测试1:像二柱子那样,花二十分钟写一个能自动生成30道小学四则运算题目的 “软件”
课堂测试2: (1)题目避免重复; (2)可定制(数量/打印方式);
课堂测试3: 2、可定制(数量/打印方式):输入大的数量值,测试一下系统是否崩溃,反向查找系统是否优化的余地; 3、定制操作数的个数:4、定制是否有乘除法 5、定制是否有括号(随机加入) 6 、定制数值范围(确定操作数的取值范围)
1 package kaoshi; 2 3 import java.util.Random; 4 import java.util.Scanner; 5 6 public class erzhuzigan { 7 public static void main(String[] args) { 8 zuoYe(); //调用作业函数 9 } 10 11 public static void shuZi() { 12 Random random=new Random(); 13 int a=random.nextInt(100)+1; 14 System.out.print(a); 15 } //定义数字函数,并且输出随机数 16 17 public static void fuHao() { 18 Random random=new Random(); 19 int a=random.nextInt(4); 20 if(a==0){ 21 System.out.print("+"); 22 } 23 if(a==1){ 24 System.out.print("-"); 25 } 26 if(a==2){ 27 System.out.print("*"); 28 } 29 if(a==3){ 30 System.out.print("/"); 31 } 32 } //定义符号函数,通过1~4的随机数控制加减乘除的输出 33 34 public static void zuoYe() { 35 Scanner sc=new Scanner(System.in); 36 Random random=new Random(); 37 System.out.println("请输入操作数:"); 38 int b=sc.nextInt(); 39 System.out.println("请输入题目个数:"); 40 int a=sc.nextInt(); 41 for(int i=0;i<a;i++){ 42 int c=random.nextInt(5); 43 if(c!=0) { 44 shuZi(); 45 for (int j = 0; j < b - 1; j++) { 46 if (j < b - 2) { //随机数给其他位置分配括号 47 int d=random.nextInt(5); 48 if (d == 1) { 49 fuHao(); 50 System.out.print("("); 51 shuZi(); 52 fuHao(); 53 shuZi(); 54 System.out.print(")"); 55 j++; 56 } 57 else { 58 fuHao(); 59 shuZi(); 60 } 61 } 62 else { 63 fuHao(); 64 shuZi(); 65 } 66 } 67 } //随机数判断是否给第一个数括号 68 else{ 69 System.out.print("("); 70 shuZi(); 71 fuHao(); 72 shuZi(); 73 System.out.print(")"); 74 for (int j = 1; j < b - 1; j++) { 75 if (j < b - 2) { 76 int d=random.nextInt(5); 77 if (d == 1) { 78 fuHao(); 79 System.out.print("("); 80 shuZi(); 81 fuHao(); 82 shuZi(); 83 System.out.print(")"); 84 j++; 85 } 86 else { 87 fuHao(); 88 shuZi(); 89 } 90 } 91 else { 92 fuHao(); 93 shuZi(); 94 } 95 } 96 } 97 System.out.println("="); 98 } //循环输出操作数 99 } //定义作业函数 100 }
用了比较笨的方法,但是总体思路还算是比较清晰的,明天把二柱子和论文两个加上注释,读者们就更方便理解了。
浙公网安备 33010602011771号