生成随机四则运算

代码部分:

package lianxi;
import java.util.Scanner;
public class reduam {

public static void main(String[] args) {
Scanner in=new Scanner(System.in);
suijishu(getchutishu(),getcaozuoshugeshu(),getmax(),getmin());
}
public static void suijishu(int tishu,int caozuoshugeshu,int min,int max) {
Integer[][] r=new Integer[tishu][caozuoshugeshu];
Integer[][] t=new Integer[tishu][caozuoshugeshu];
int i=0,j,h,h1=0,h2=0;
int total=0,s=0;
Integer[] t1=new Integer[tishu];
Scanner in=new Scanner(System.in);
Character[][] x=new Character[tishu][caozuoshugeshu];
for(i=0;i<tishu;i++) {
for(j=0;j<caozuoshugeshu;j++) {
r[i][j]=(int)(Math.random()*(max-min)+min);
}
for(j=0;j<caozuoshugeshu-1;j++) {
t[i][j]=(int)(Math.random()*4+1);
}
t[i][caozuoshugeshu-1]=32;
for(j=0;j<caozuoshugeshu;j++) {
switch(t[i][j]) {
case 1:x[i][j]='+';break;
case 2:x[i][j]='-';break;
case 3:x[i][j]='*';break;
case 4:x[i][j]='/';break;
case 32:x[i][j]='=';break;
}
chachong(r,x,caozuoshugeshu,i);
}
total=r[i][0];
System.out.print((i+1)+":");
for(j=0;j<caozuoshugeshu;j++) {
System.out.print(r[i][j]+""+x[i][j]);
}
for(j=0;j<caozuoshugeshu;j++) {

while((x[i][j]=='*'||x[i][j]=='/')&&j<caozuoshugeshu){

if(x[i][j]=='*') {
total*=r[i][j+1];
}
else {
total/=r[i][j+1];

}
j++;
}
if(x[i][j]=='+'||x[i][j]=='-') {
int u=r[i][j+1];
if(x[i][j]=='+') {

while((x[i][j+1]=='*'||x[i][j+1]=='/')&&(j+1)<caozuoshugeshu){

if(x[i][j+1]=='*') {
u*=r[i][j+2];
}
else {
u/=r[i][j+2];

}
j++;
}
total+=u;
}

else if(x[i][j]=='-') {

while((x[i][j+1]=='*'||x[i][j+1]=='/')&&(j+1)<caozuoshugeshu){

if(x[i][j+1]=='*') {
u*=r[i][j+2];
}
else {
u/=r[i][j+2];

}
j++;
}
total-=u;
}

}
}
h=in.nextInt();
System.out.println();
System.out.println(total);
if(h==total) {
System.out.println("答对了");
h1++;
}
else {
System.out.println("答错了");
h2++;
t1[s++]=i;

}
}
System.out.println("答对了"+h1+"道题,答错了"+h2+"道题");
System.out.println("正确率:"+((double)h1/(double)tishu));
System.out.println("错题有:");
for(i=0;i<s;i++) {
for(j=0;j<caozuoshugeshu;j++) {
System.out.print(r[t1[i]][j]+""+x[t1[i]][j]);
}
System.out.println();
}
cuotiji(t1,r,x,tishu,caozuoshugeshu,s);

}
public static int getchutishu() {
Scanner in=new Scanner(System.in);
return in.nextInt();
}
public static int getcaozuoshugeshu() {
Scanner in=new Scanner(System.in);
return in.nextInt();
}
public static int getmax() {
Scanner in=new Scanner(System.in);
return in.nextInt();
}
public static int getmin() {
Scanner in=new Scanner(System.in);
return in.nextInt();
}
public static void chachong(Integer[][] q,Character[][]w,int caozuoshugeshu,int p) {
int i,j;
for(i=0;i<p;i++) {
for(j=0;j<caozuoshugeshu;j++) {
if(q[i][j]==q[p][j]||w[i][j]==w[p][j])
break;
}

}
}
public static void cuotiji(Integer[] t1,Integer[][] r,Character[][] x,int tishu,int caozuoshugeshu,int s) {
Scanner in=new Scanner(System.in);
int k,h;int i,j;
Integer[] t0=new Integer[s];
for(i=0;i<s;i++) {
t0[i]=1;
}
System.out.println("请输入你想进行的操作");
k=in.nextInt();
switch(k) {
case 1:System.out.println("错题有");

for(i=0;i<s;i++) {
for(j=0;j<caozuoshugeshu;j++) {
System.out.print(r[t1[i]][j]+""+x[t1[i]][j]);
}
System.out.println();
}
break;
case 2:System.out.println("错题重练");
int total;
for(i=0;i<s;i++) {
total=r[t1[i]][0];
for(j=0;j<caozuoshugeshu;j++) {
System.out.print(r[t1[i]][j]+""+x[t1[i]][j]);
}
for(j=0;j<caozuoshugeshu;j++) {
while((x[t1[i]][j]=='*'||x[t1[i]][j]=='/')&&j+2<caozuoshugeshu){

if(x[t1[i]][j]=='*') {
total*=r[t1[i]][j+1];
}
else {
total/=r[t1[i]][j+1];

}
j++;
}
if(x[t1[i]][j]=='+'||x[t1[i]][j]=='-') {
int u=r[t1[i]][j+1];
if(x[t1[i]][j]=='+') {

while((x[t1[i]][j+1]=='*'||x[t1[i]][j+1]=='/')&&(j+2)<caozuoshugeshu){

if(x[t1[i]][j+1]=='*') {
u*=r[t1[i]][j+2];
}
else {
u/=r[t1[i]][j+2];

}
j++;
}
total+=u;
}

else if(x[t1[i]][j]=='-') {

while((x[t1[i]][j+1]=='*'||x[t1[i]][j+1]=='/')&&(j+2)<caozuoshugeshu){

if(x[t1[i]][j+1]=='*') {
u*=r[t1[i]][j+2];
}
else {
u/=r[t1[i]][j+2];

}
j++;
}
total-=u;
}

}
}
h=in.nextInt();
System.out.println();
System.out.println(total);
if(h==total) {
System.out.println("答对了");
}
else {
System.out.println("答错了"+(++t0[i])+"次");

}
System.out.println();

}

}
}

}

运行结果

 程序还不好,当没有错题时,也会提醒错题有,只是没有东西;

原文地址:https://www.cnblogs.com/092e/p/14142359.html