2021.9.17课堂小测试完善
对上次小测试的完善,改进了随机数前面重复问题以及括号出现问题。并把方法根据要求应用到所有函数中。
代码实现:
import java.util.*;
public class shiyan {
public static void main(String[] args) {
System.out.println("请输入要出题目的数量:");
Scanner sc=new Scanner(System.in);
int i=sc.nextInt();
System.out.println("是否需要乘除法,填YES或NO:");
Scanner in=new Scanner(System.in);
String s=in.nextLine();
System.out.println("请输入算式数字范围:");
int c=sc.nextInt();
System.out.println("是否需要括号,填YES或NO");
String u=in.nextLine();
System.out.println("请输入操作数的个数:");
int z=sc.nextInt();
String b=new String("YES");
String x=new String("NO");
if(s.equals(b)) {
if(u.equals(b))
xuChengKuo(i,c,z);
else xuChengbuKuo(i,c,z);
}
else
if(s.equals(x)) {
if(u.equals(b)) buChengKuo(i,c,z);
else buChengbuKuo(i,c,z);
}
}
public static void xuChengKuo(int i,int c,int z) //i是题目数量,c是算式范围,z是操作数
{
Random random=new Random();
for(int j=0;j<i;j++) {
int k1,k2,k3;
int k=0;
int cao[]=new int[z];
int fu1[]=new int[z-1];
String fu2[]=new String[z-1];
int x1,x2;
k1=random.nextInt(z-1);
k2=random.nextInt(z-1);
if(k1>k2) {
k3=k1;
k1=k2;
k2=k3;
}
k3=random.nextInt(2);
if(k2-k1==1||k1-k2==0) k3=0;
for(x1=0;x1<z;x1++) {
int a=random.nextInt(c);
for(int l=0;l<x1;l++)
if(a==cao[l]) k++;
if(k!=0) continue;
cao[x1]=a+1;
}
for(x2=0;x2<z-1;x2++) {
int b=random.nextInt(4);
fu1[x2]=b;
if(fu1[x2]==0) {
fu2[x2]="+";
}
if(fu1[x2]==1) {
fu2[x2]="-";
}
if(fu1[x2]==2) {
fu2[x2]="*";
}
if(fu1[x2]==3) {
fu2[x2]="/";
}
}
for(int j1=0;j1<z-1;j1++) {
if(k3==0) {
System.out.print(cao[j1]);
System.out.print(fu2[j1]);
}
else {if(k1==j1) System.out.print("(");
System.out.print(cao[j1]);
if(k2==j1) System.out.print(")");
System.out.print(fu2[j1]);
}
}
if(k2==z-1) System.out.println(cao[z-1]+")");
else System.out.println(cao[z-1]);
k3=1;
}
}
public static void xuChengbuKuo(int i,int c,int z) {
Random random=new Random();
for(int j=0;j<i;j++) {
int k1,k2,k3;
int k=0;
int cao[]=new int[z];
int fu1[]=new int[z-1];
String fu2[]=new String[z-1];
int x1,x2;
k1=random.nextInt(z-1);
k2=random.nextInt(z-1);
if(k1>k2) {
k3=k1;
k1=k2;
k2=k3;
}
for(x1=0;x1<z;x1++) {
int a=random.nextInt(c);
for(int l=0;l<x1;l++)
if(a==cao[l]) k++;
if(k!=0) continue;
cao[x1]=a+1;
}
for(x2=0;x2<z-1;x2++) {
int b=random.nextInt(4);
fu1[x2]=b;
if(fu1[x2]==0) {
fu2[x2]="+";
}
if(fu1[x2]==1) {
fu2[x2]="-";
}
if(fu1[x2]==2) {
fu2[x2]="*";
}
if(fu1[x2]==3) {
fu2[x2]="/";
}
}
for(int j1=0;j1<z-1;j1++) {
System.out.print(cao[j1]);
System.out.print(fu2[j1]);
}
System.out.println(cao[z-1]);
}
}
public static void buChengKuo(int i,int c,int z){
Random random=new Random();
for(int j=0;j<i;j++) {
int k1,k2,k3;
int k=0;
int cao[]=new int[z];
int fu1[]=new int[z-1];
String fu2[]=new String[z-1];
int x1,x2;
k1=random.nextInt(z-1);
k2=random.nextInt(z-1);
if(k1>k2) {
k3=k1;
k1=k2;
k2=k3;
}
k3=random.nextInt(2);
if(k2-k1==1||k1-k2==0) k3=0;
for(x1=0;x1<z;x1++) {
int a=random.nextInt(c);
for(int l=0;l<x1;l++)
if(a==cao[l]) k++;
if(k!=0) continue;
cao[x1]=a+1;
}
for(x2=0;x2<z-1;x2++) {
int b=random.nextInt(2);
fu1[x2]=b;
if(fu1[x2]==0) {
fu2[x2]="+";
}
if(fu1[x2]==1) {
fu2[x2]="-";
}
}
for(int j1=0;j1<z-1;j1++) {
if(k3==0) {
System.out.print(cao[j1]);
System.out.print(fu2[j1]);
}
else {if(k1==j1) System.out.print("(");
System.out.print(cao[j1]);
if(k2==j1) System.out.print(")");
System.out.print(fu2[j1]);
}
}
if(k2==z-1) System.out.println(cao[z-1]+")");
else System.out.println(cao[z-1]);
k3=1;
}
}
public static void buChengbuKuo(int i,int c,int z){
Random random=new Random();
for(int j=0;j<i;j++) {
int k1,k2,k3;
int k=0;
int cao[]=new int[z];
int fu1[]=new int[z-1];
String fu2[]=new String[z-1];
int x1,x2;
k1=random.nextInt(z-1);
k2=random.nextInt(z-1);
if(k1>k2) {
k3=k1;
k1=k2;
k2=k3;
}
for(x1=0;x1<z;x1++) {
int a=random.nextInt(c);
for(int l=0;l<x1;l++)
if(a==cao[l]) k++;
if(k!=0) continue;
cao[x1]=a+1;
}
for(x2=0;x2<z-1;x2++) {
int b=random.nextInt(2);
fu1[x2]=b;
if(fu1[x2]==0) {
fu2[x2]="+";
}
if(fu1[x2]==1) {
fu2[x2]="-";}
}
for(int j1=0;j1<z-1;j1++) {
System.out.print(cao[j1]);
System.out.print(fu2[j1]);
}
System.out.println(cao[z-1]);
}
}
}
运行结果: