9.17课堂小测试
题目要求:
代码实现:
import java.util.Random;
import java.util.*;
import java.util.Scanner;
public class yunsuan {
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();
if(z>3) {System.out.println("输入错误");}
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) { //函数需要乘除法以及括号的实现。
int cun1[];
int cun2[];
int cun3[];
cun1=new int[i];
cun2=new int[i];
cun3=new int[i];
Random random=new Random();
for(int j=0;j<i;j++) {
int random1=random.nextInt(4);
int random2=random.nextInt(c);
int random3=random.nextInt(c);
int random4=random.nextInt(c);
int random5=random.nextInt(2);
int random6=random.nextInt(2);
int random7=random.nextInt(4);
String k1="+",k2="-";
if(z==2) {
if(random1==0)
{ k1=new String("+");}
else if(random1==1) { k1=new String("-");}
else if(random1==2) { k1=new String("*");}
else if(random1==3) { k1=new String("/");}
}
else if(z==3) {
if(random1==0) {
if(random7==0) { k1=new String("+"); k2=new String("+");}
if(random7==1) { k1=new String("+"); k2=new String("-");}
if(random7==2) { k1=new String("+"); k2=new String("*");}
if(random7==3) { k1=new String("+"); k2=new String("/");}
}
if(random1==1) {
if(random7==0) { k1=new String("-"); k2=new String("+");}
if(random7==1) { k1=new String("-"); k2=new String("-");}
if(random7==2) { k1=new String("-"); k2=new String("*");}
if(random7==3) { k1=new String("-"); k2=new String("/");}
}
if(random1==2) {
if(random7==0) { k1=new String("*"); k2=new String("+");}
if(random7==1) { k1=new String("*"); k2=new String("-");}
if(random7==2) { k1=new String("*"); k2=new String("*");}
if(random7==3) { k1=new String("*"); k2=new String("/");}
}
if(random1==3) {
if(random7==0) { k1=new String("/"); k2=new String("+");}
if(random7==1) { k1=new String("/"); k2=new String("-");}
if(random7==2) { k1=new String("/"); k2=new String("*");}
if(random7==3) { k1=new String("/"); k2=new String("/");}
}
}
cun1[j]=random1;
cun2[j]=random2;
cun3[j]=random3;
for(int k=0;k<j;k++) {
if(cun1[j]==cun1[k]&&cun2[j]==cun2[k]&&cun3[j]==cun3[k]&&cun1[j]==cun3[k]&&cun3[j]==cun1[k])
continue;
}
switch(random1) {
case 0: if(z==2) {if(random5==0) {System.out.println(random2+k1+random3+"=");break;}
else {System.out.println("("+random2+k1+random3+")"+"=");break;}
}
else if(z==3) {
if(random5==0) {System.out.println(random2+k1+random3+k2+random4+"=");break;}
else if(random6==0) {System.out.println("("+random2+k1+random3+")"+k2+random4+"=");break;}
else {System.out.println("("+random2+k1+random3+")"+k2+random4+"=");break;}
}
case 1:if(z==2) {if(random5==0) {System.out.println(random2+k1+random3+"=");break;}
else {System.out.println("("+random2+k1+random3+")"+"=");break;}
}
else if(z==3) {
if(random5==0) {System.out.println(random2+k1+random3+k2+random4+"=");break;}
else if(random6==0) {System.out.println("("+random2+k1+random3+")"+k2+random4+"=");break;}
else {System.out.println("("+random2+k1+random3+")"+k2+random4+"=");break;}
}
case 2:if(z==2) {if(random5==0) {System.out.println(random2+k1+random3+"=");break;}
else {System.out.println("("+random2+k1+random3+")"+"=");break;}
}
else if(z==3) {
if(random5==0) {System.out.println(random2+k1+random3+k2+random4+"=");break;}
else if(random6==0) {System.out.println("("+random2+k1+random3+")"+k2+random4+"=");break;}
else {System.out.println("("+random2+k1+random3+")"+k2+random4+"=");break;}
}
case 3:if(z==2) {if(random5==0) {System.out.println(random2+k1+random3+"=");break;}
else {System.out.println("("+random2+k1+random3+")"+"=");break;}
}
else if(z==3) {
if(random5==0) {System.out.println(random2+k1+random3+k2+random4+"=");break;}
else if(random6==0) {System.out.println("("+random2+k1+random3+")"+k2+random4+"=");break;}
else {System.out.println("("+random2+k1+random3+")"+k2+random4+"=");break;}
}
}
}
}
public static void xuChengbuKuo(int i,int c,int z) { //函数需要乘除法不需要括号的实现。
int cun1[];
int cun2[];
int cun3[];
cun1=new int[i];
cun2=new int[i];
cun3=new int[i];
Random random=new Random();
for(int j=0;j<i;j++) {
int random1=random.nextInt(4);
int random2=random.nextInt(c);
int random3=random.nextInt(c);
int random4=random.nextInt(c);
int random7=random.nextInt(4);
String k1="+",k2="-";
if(z==2) {
if(random1==0)
{ k1=new String("+");}
else if(random1==1) { k1=new String("-");}
else if(random1==2) { k1=new String("*");}
else if(random1==3) { k1=new String("/");}
}
else if(z==3) {
if(random1==0) {
if(random7==0) { k1=new String("+"); k2=new String("+");}
if(random7==1) { k1=new String("+"); k2=new String("-");}
if(random7==2) { k1=new String("+"); k2=new String("*");}
if(random7==3) { k1=new String("+"); k2=new String("/");}
}
if(random1==1) {
if(random7==0) { k1=new String("-"); k2=new String("+");}
if(random7==1) { k1=new String("-"); k2=new String("-");}
if(random7==2) { k1=new String("-"); k2=new String("*");}
if(random7==3) { k1=new String("-"); k2=new String("/");}
}
if(random1==2) {
if(random7==0) { k1=new String("*"); k2=new String("+");}
if(random7==1) { k1=new String("*"); k2=new String("-");}
if(random7==2) { k1=new String("*"); k2=new String("*");}
if(random7==3) { k1=new String("*"); k2=new String("/");}
}
if(random1==3) {
if(random7==0) { k1=new String("/"); k2=new String("+");}
if(random7==1) { k1=new String("/"); k2=new String("-");}
if(random7==2) { k1=new String("/"); k2=new String("*");}
if(random7==3) { k1=new String("/"); k2=new String("/");}
}
}
cun1[j]=random1;
cun2[j]=random2;
cun3[j]=random3;
for(int k=0;k<j;k++) {
if(cun1[j]==cun1[k]&&cun2[j]==cun2[k]&&cun3[j]==cun3[k]&&cun1[j]==cun3[k]&&cun3[j]==cun1[k])
continue;
}
switch(random1) {
case 0:if(z==2) {System.out.println(random2+k1+random3+"=");break;}
if(z==3) {System.out.println(random2+k1+random3+k2+random4+"=");break;}
case 1:if(z==2) {System.out.println(random2+k1+random3+"=");break;}
else if(z==3) {System.out.println(random2+k1+random3+k2+random4+"=");break;}
case 2: if(z==2) {System.out.println(random2+k1+random3+"=");break;}
else if(z==3) {System.out.println(random2+k1+random3+k2+random4+"=");break;}
case 3:if(z==2) {System.out.println(random2+k1+random3+"=");break;}
else if(z==3) {System.out.println(random2+k1+random3+k2+random4+"=");break;}
}
}
}
public static void buChengKuo(int i,int c,int z){ //函数不需要乘除法需要括号的实现。
int cun1[];
int cun2[];
int cun3[];
cun1=new int[i];
cun2=new int[i];
cun3=new int[i];
Random random=new Random();
for(int j=0;j<i;j++) {
int random1=random.nextInt(2);
int random2=random.nextInt(c);
int random3=random.nextInt(c);
int random4=random.nextInt(c);
int random5=random.nextInt(2);
int random6=random.nextInt(2);
int random7=random.nextInt(2);
String k1="+",k2="-";
if(z==2) {
if(random1==0)
{ k1=new String("+");}
else if(random1==1) { k1=new String("-");}
}
else if(z==3) {
if(random1==0) {
if(random7==0) { k1=new String("+"); k2=new String("+");}
if(random7==1) { k1=new String("+"); k2=new String("-");}
}
if(random1==1) {
if(random7==0) { k1=new String("-"); k2=new String("+");}
if(random7==1) { k1=new String("-"); k2=new String("-");}
}
}
cun1[j]=random1;
cun2[j]=random2;
cun3[j]=random3;
for(int k=0;k<j;k++) {
if(cun1[j]==cun1[k]&&cun2[j]==cun2[k]&&cun3[j]==cun3[k]&&cun1[j]==cun3[k]&&cun3[j]==cun1[k])
continue;
}
switch(random1) {
case 0:if(z==2) {if(random5==0) {System.out.println(random2+k1+random3+"=");break;}
else {System.out.println("("+random2+k1+random3+")"+"=");break;}
}
else if(z==3) {
if(random5==0) {System.out.println(random2+k1+random3+k2+random4+"=");break;}
else if(random6==0) {System.out.println("("+random2+k1+random3+")"+k2+random4+"=");break;}
else {System.out.println("("+random2+k1+random3+")"+k2+random4+"=");break;}
}
case 1:if(z==2) {if(random5==0) {System.out.println(random2+k1+random3+"=");break;}
else {System.out.println("("+random2+k1+random3+")"+"=");break;}
}
else if(z==3) {
if(random5==0) {System.out.println(random2+k1+random3+k2+random4+"=");break;}
else if(random6==0) {System.out.println("("+random2+k1+random3+")"+k2+random4+"=");break;}
else {System.out.println("("+random2+k1+random3+")"+k2+random4+"=");break;}
}
}
}
}
public static void buChengbuKuo(int i,int c,int z){ //函数不需要乘除法不需要括号的实现。
int cun1[];
int cun2[];
int cun3[];
cun1=new int[i];
cun2=new int[i];
cun3=new int[i];
Random random=new Random();
for(int j=0;j<i;j++) {
int random1=random.nextInt(2);
int random2=random.nextInt(c);
int random3=random.nextInt(c);
int random4=random.nextInt(c);
int random7=random.nextInt(2);
String k1="+",k2="-";
if(z==2) {
if(random1==0)
{ k1=new String("+");}
else if(random1==1) { k1=new String("-");}
}
else if(z==3) {
if(random1==0) {
if(random7==0) { k1=new String("+"); k2=new String("+");}
if(random7==1) { k1=new String("+"); k2=new String("-");}
}
if(random1==1) {
if(random7==0) { k1=new String("-"); k2=new String("+");}
if(random7==1) { k1=new String("-"); k2=new String("-");}
}
}
cun1[j]=random1;
cun2[j]=random2;
cun3[j]=random3;
for(int k=0;k<j;k++) {
if(cun1[j]==cun1[k]&&cun2[j]==cun2[k]&&cun3[j]==cun3[k]&&cun1[j]==cun3[k]&&cun3[j]==cun1[k])
continue;
}
switch(random1) {
case 0:if(z==2) {System.out.println(random2+k1+random3+"=");break;}
else if(z==3) {System.out.println(random2+k1+random3+k2+random4+"=");break;}
case 1:if(z==2) {System.out.println(random2+k1+random3+"=");break;}
else if(z==3) {System.out.println(random2+k1+random3+k2+random4+"=");break;}
}
}
}
}
运行结果:
收获:
这次的题目解答,让我学会了随机数random类的使用,以及许多细节处理,但是这个程序只能输出3个数字的运算题目,不能进行更多位数的运算。除此之外,它的程序实现显得太长,要尝试进行算法的优化,但是我现在不具备这种能力,在以后的学习中,我会更加努力。