今天完成了四则运算出题的第三阶段任务
继续写昨天的论文管理系统
下面这个程序就是一个出四则运算题的系统
package sizeys; import java.util.Random; import java.lang.Math; import java.util.Scanner; public class Randomtest { public static int[] randomCommon(int min, int max, int n){ if (n > (max - min + 1) || max < min) { return null; } int[] result = new int[n]; int count = 0; while(count < n) { int num = (int) (Math.random() * (max - min)) + min; boolean flag = true; for (int j = 0; j < n; j++) { if(num == result[j]){ flag = false; break; } } if(flag){ result[count] = num; count++; } } return result; } public static char operator(int n,int se) { char op=' '; Random random = new Random(); n = random.nextInt(100); if(se==1) { switch(n%4) { case 0:op = '+';break; case 1:op = '-';break; case 2:op = '*';break; case 3:op = '/';break; } } if(se==2) { switch(n%2) { case 0:op = '+';break; case 1:op = '-';break; } } return op; } public static void main(String[] args) { Random random = new Random(); int a,b,c,d,n,op; //b随机第一个运算符 op随机第二个运算符 n出题个数 int i,select1,select2,select3,brackets; //select1选择乘除法 select2选择括号运算 select3输入操作数个数 brackets随机括号位置 int min,max; Scanner sc = new Scanner(System.in); System.out.println("请输入出题个数:"); n = sc.nextInt(); System.out.println("请输入任意数范围:"); System.out.println("min="); min = sc.nextInt(); System.out.println("max="); max = sc.nextInt(); System.out.println("请输入操作数个数:"); select3 = sc.nextInt(); System.out.println("是否需要乘除法:1.需要 2.不需要"); select1 = sc.nextInt(); System.out.println("是否需要括号运算:1.需要 2.不需要"); select2 = sc.nextInt(); int[] arr = randomCommon(min,max,n*select3); if(n*select3>(max-min+1)) { System.out.println("超出范围!"); System.exit(0); } else { if(select3==2) { if(select1==1) { for(i=0;i<n*select3;i++) { b = random.nextInt(100); a = arr[i]; c = arr[i+1]; char op3 = operator(b,select1); System.out.println(""+a+op3+c); i++; } } else if(select1==2) { for(i=0;i<n*select3;i++) { b = random.nextInt(100); a = arr[i]; c = arr[i+1]; char op3 = operator(b,select1); System.out.println(""+a+op3+c); i++; } } } if(select3==3) { if(select1==1) { if(select2==1) { for(i=0;i<select3*n;i++) { b = random.nextInt(100); op = random.nextInt(100); brackets = random.nextInt(100); a = arr[i]; c = arr[i+1]; d = arr[i+2]; char op1 = operator(b,select1); char op2 = operator(op,select1); switch(brackets%2) { case 0:System.out.println("("+a+op1+c+")"+op2+d);break; case 1:System.out.println(""+a+op1+"("+c+op2+d+")");break; } i++; i++; } } if(select2==2) { for(i=0;i<select3*n;i++) { b = random.nextInt(100); op = random.nextInt(100); a = arr[i]; c = arr[i+1]; d = arr[i+2]; char op1 = operator(b,select1); char op2 = operator(op,select1); System.out.println(""+a+op1+c+op2+d); i++; i++; } } }//需要乘除法select1==1 else if(select1==2) { if(select2==1) { for(i=0;i<n*3;i++) { b = random.nextInt(100); op = random.nextInt(100); brackets = random.nextInt(100); a = arr[i]; c = arr[i+1]; d = arr[i+2]; char op1 = operator(b,select1); char op2 = operator(op,select1); switch(brackets%2) { case 0:System.out.println("("+a+op1+c+")"+op2+d);break; case 1:System.out.println(""+a+op1+"("+c+op2+d+")");break; } i+=2; } } if(select2==2) { for(i=0;i<n*3;i++) { b = random.nextInt(100); op = random.nextInt(100); a = arr[i]; c = arr[i+1]; d = arr[i+2]; char op1 = operator(b,select1); char op2 = operator(op,select1); System.out.println(""+a+op1+c+op2+d); i+=2; } } }//不需要乘除法select1==2 }//3个操作数 }//范围内 }//main() }//class