出四则运算题,可任意操作数
应付作业写的漏洞太多谨慎观看
import java.util.Random; import java.lang.Math; import java.util.Scanner; public class RandomCommon { public static int[] randomCommon(int min, int max, int sumoprand){ int sum = sumoprand; if (sum > (max - min + 1) || max < min) { return null; } int[] result = new int[sum]; int count = 0; while(count < sum) { int num = (int) (Math.random() * (max - min)) + min; boolean flag = true; for (int j = 0; j < sum; 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) { int sumtitle;//出题个数 int sumoperand;//操作数个数 int min,max; int chengchu; Scanner sc = new Scanner(System.in); System.out.println("请输入出题个数:"); sumtitle = sc.nextInt(); System.out.println("请输入操作数个数:"); sumoperand = sc.nextInt(); System.out.println("请输入任意数范围:"); System.out.println("min="); min = sc.nextInt(); System.out.println("max="); max = sc.nextInt(); System.out.println("是否需要乘除法:1.需要 2.不需要"); chengchu = sc.nextInt(); String[] str = new String[sumtitle]; String str0; Random random = new Random(); for(int i=0;i<sumtitle;i++) { int a = random.nextInt(max); int b = random.nextInt(max); int cc = random.nextInt(100); char c = operator(cc,chengchu); str0 = ""+a+c+b; str[i] = problem(str0,sumoperand,min,max,sumoperand,chengchu); } for(int i=0;i<sumtitle;i++) { System.out.println(str[i]+"\n"); } } public static String problem(String str,int n,int min,int max,int sumoprand,int chengchu) { Random random = new Random(); int b = random.nextInt(100); int[] arr = randomCommon(min,max,sumoprand); for(int i=2;i<n;i++) { char c = operator(b,chengchu); str = str + c + arr[i]; } return str; } }