Realize random generating formulas, support brackets and four arithmetic operations.

Analysis: Realize random generation of calculation formulas, support brackets and four arithmetic operations.

Idea: First generate a random character array, then generate a random array, and concatenate the two. Supports the generation of a specified range and the length of the specified calculation formula.

Code
package com.instance;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Create_title

public String create_title( int range, int num2) //num: number of mentions range: range num2: number of titles

Random random = new Random(range);

Stringchar_arrays = null;
int num3 = (int) (Math.random() * num2);//
Number of fractions int num4 = num2-num3;//Number of positive numbers
int charNum = num2-1;//Number of symbols number

int bracketL = 0;//(location

int int_arrays = new intnum4 + num3 * 2;
boolean flag = isBracket();
if(flag)
char_arrays = returnChar(num3,charNum+2,flag);

}else
char_arrays = returnChar(num3,charNum,false);
}

for(int w=0;w<num2;wint_arraysw = random.nextInt(range);
}
String title = “”;
if(char_arrays0.equals("("))
for(int i = 0;i<num2;ititle +=char_arraysi+((int)(Math.random()*range)+"");
}
if(char_arraysnum2.equals(")"))
title += “)”;
}

}else
for (int i = 0; i <= num2; ititle += ((int) (Math.random() * range) + “”) + char_arraysi;
}
}
if(title.charAt(title.length()-1)’+'title.charAt(title.length()-1)’/'title = title.substring(0,title.length()-1);
}

return title;
}

boolean isBracket()
return true;
}

String returnChar(int mark,int charNum,boolean flag)
Random random = new Random();
String char_array = new StringcharNum;

if(flag)
int bracketL = random.nextInt(charNum-2);//(The position, the last digit and the penultimate digit cannot be (

int j = charNum - bracketL - 2;

int bracketR = bracketL+random.nextInt(j) + 2;//) This random number in position can generate 0

if(bracketL!=0)
char_arraybracketL = “+(”;
}else
char_arraybracketL = “(”;

}

if(bracketRcharNum-1)
char_arraybracketR = “)”;
}else
char_arraybracketR = “)-”;
}
}
int mark1 = 0;
while(true)
if(mark1
mark)
break;
}
int i = (int)(Math.random()*charNum);
if(char_arrayi==null)
char_arrayi="/";
mark1}

}
int num = 0;
while(true)

if(num == charNum)break;
if(char_arraynum == null)
int i= (int)(Math.random()2);
switch (i)
case 0:char_arraynum="
";
break;
case 1:char_arraynum="+";
break;
case 2:char_arraynum="-";
break;
}
}

num
}
return char_array;
}

}
Call
Insert picture description here
[result display
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_44688861/article/details/105073804