测试是三个阶段,一阶段要求30个输出,二阶段要求控制输出数目以及不重复,三阶段要求控制括号,控制操作数,控制乘除法。
下面两段代码,一二阶段用的是同一段代码,操作数都是2个数字,第三阶段因为操作数目的变化所以改变思路,使用的另外一种新的思路。
import java.util.Random; import java.util.Scanner; public class sizeyunsuan { public static void main(String[] args) { String s[]=new String[100]; int c; Scanner c1=new Scanner(System.in); c=c1.nextInt(); for (int i=0;i<c;i++){ Random r1 = new Random(); int i1 = r1.nextInt(99) + 1; Random r2 = new Random(); int i2 = r2.nextInt(3) + 1; Random r3 = new Random(); int i3 = r3.nextInt(99) + 1; if(i1==i2) {continue;} else { if (i2 == 1) { String s1="i1 + \"+\" + i2 + \"=\""; for(int j=0;j<i;j++){ if(s1!=s[j]){ System.out.println(i1 + "+" + i2 + "="); s[i]="i1 + \"+\" + i2 + \"=\""; break; } } } if (i2 == 2) { String s1="i1 + \"-\" + i2 + \"=\""; for(int j=0;j<i;j++){ if(s1!=s[j]){ System.out.println(i1 + "-" + i2 + "="); s[i]="i1 + \"-\" + i2 + \"=\""; break; }} } if (i2 == 3) { String s1="i1 + \"*\" + i2 + \"=\""; for(int j=0;j<i;j++){ if(s1!=s[j]){ System.out.println(i1 + "*" + i2 + "="); s[i]="i1 + \"*\" + i2 + \"=\""; break; }} } if (i2 == 4) { String s1="i1 + \"/\" + i2 + \"=\""; for(int j=0;j<i;j++){ if(s1!=s[j]){ System.out.println(i1 + "/" + i2 + "="); s[i]="i1 + \"/\" + i2 + \"=\""; break; }} } } } } }
import java.lang.reflect.Array; import java.util.Random; import java.util.Scanner; //先输入需要多少操作数,然后开始生成随机数,一个随机数一个运算符,加计数器,直到运算数的数目达到要求,运用字符串的常用函数,把新生成的加入到之前的后面 ,最后是输出字符串,未完成目前 //避免重复是存储字符串,存储然后用equals对比 //随机生成括号,创建随机数在输出某个操作数之后,随机生成括号位置,左括号通过使得随机数在最后一个操作数减去2之前生成,右括号要求至少在左括号之后的一个操作数之后输出。 //控制乘除法可以通过控制随机数1.2之间 public class sizeyunsuan { public static void main(String[] args) {int c; System.out.println("请输入题目个数:"); Scanner c1 = new Scanner(System.in); c = c1.nextInt(); int a; System.out.println("请输入操作数(大于2):"); Scanner a1 = new Scanner(System.in); a = a1.nextInt(); int x, y; System.out.println("是否需要括号(操作数大于3):1.是 2.否"); Scanner x1 = new Scanner(System.in); x = x1.nextInt(); System.out.println("是否有乘除法:1.是 2.否"); Scanner y1 = new Scanner(System.in); y = y1.nextInt(); if (x == 2 && y == 1) { for (int j = 0; j < c; j++) { for (int i = 0; i < a - 1; i++) { Random r1 = new Random(); int i1 = r1.nextInt(99) + 1; Random r2 = new Random(); int i2 = r2.nextInt(3) + 1; char fuhao = ‘0‘; if (i2 == 1) { fuhao = ‘+‘; } else if (i2 == 2) { fuhao = ‘-‘; } else if (i2 == 3) { fuhao = ‘*‘; } else if (i2 == 4) { fuhao = ‘/‘; } System.out.print(i1); System.out.print(fuhao); } Random r1 = new Random(); int i1 = r1.nextInt(99) + 1; System.out.println(i1 + "="); } } else if (x == 2 && y == 2) { for (int j = 0; j < c; j++) { for (int i = 0; i < a - 1; i++) { Random r1 = new Random(); int i1 = r1.nextInt(99) + 1; Random r2 = new Random(); int i2 = r2.nextInt(1) + 1; char fuhao = ‘0‘; if (i2 == 1) { fuhao = ‘+‘; } else if (i2 == 2) { fuhao = ‘-‘; } System.out.print(i1); System.out.print(fuhao); } Random r1 = new Random(); int i1 = r1.nextInt(99) + 1; System.out.println(i1 + "="); } } else if (x == 1 && y == 1) { for (int j = 0; j < c; j++) { int flag=0; Random r3 = new Random();//r3表示左括号的生成位置 int i3 = r3.nextInt(a-1);//0到a-1个位置 Random r4 = new Random();//r4右括号的位置 int i4 = r4.nextInt(a) %(a-i3)+i3+1; for (int i = 0; i < a - 1; i++) { Random r1 = new Random(); int i1 = r1.nextInt(99) + 1; Random r2 = new Random(); int i2 = r2.nextInt(3) + 1; char fuhao = ‘0‘; if (i2 == 1) { fuhao = ‘+‘; } else if (i2 == 2) { fuhao = ‘-‘; } else if (i2 == 3) { fuhao = ‘*‘; } else if (i2 == 4) { fuhao = ‘/‘; } if(i==i3){ System.out.print("("); } System.out.print(i1); if(i==i4){ System.out.print(")"); flag=1; } System.out.print(fuhao); } Random r1 = new Random(); int i1 = r1.nextInt(99) + 1; if(flag==0){ System.out.println(i1 + ")="); } else{System.out.println(i1 + "=");} } }else if (x == 1 && y == 1) { for (int j = 0; j < c; j++) { int flag=0; Random r3 = new Random();//r3表示左括号的生成位置 int i3 = r3.nextInt(a-1);//0到a-1个位置 Random r4 = new Random();//r4右括号的位置 int i4 = r4.nextInt(a) %(a-i3)+i3+1; for (int i = 0; i < a - 1; i++) { Random r1 = new Random(); int i1 = r1.nextInt(99) + 1; Random r2 = new Random(); int i2 = r2.nextInt(1) + 1; char fuhao = ‘0‘; if (i2 == 1) { fuhao = ‘+‘; } else if (i2 == 2) { fuhao = ‘-‘; } if(i==i3){ System.out.print("("); } System.out.print(i1); if(i==i4){ System.out.print(")"); flag=1; } System.out.print(fuhao); } Random r1 = new Random(); int i1 = r1.nextInt(99) + 1; if(flag==0){ System.out.println(i1 + ")="); } else{System.out.println(i1 + "=");} } } } }
第三阶段代码的运行例子: