java课堂测试3第一部分(未完善)

2021-10-15

package test3;
import java.util.*;

public class Grade2 {

static String[][] mis=new String[500][4]; //将错误的题放入其中

public static int generateRandom(int range) //生成随机数
{
Random random=new Random();
int R=random.nextInt(range);
return R;
}

public static char generateOperator() //生成运算符
{
int i;
char a='0';
i=generateRandom(4);
switch(i)
{
case 0:a='+';break;
case 1:a='-';break;
case 2:a='*';break;
case 3:a='/';break;
}
return a;

}

public static String[][] generateProblem(int number,int range,int count) //生成算题,不带括号的
{
int i,j,k,k1=0,k2=0;
char p;
String[][] str=new String[number][count*2];
for(i=0;i<number;i++)
{
for(j=0;j<count*2-1;j+=2)
{
k=generateRandom(range);
str[i][j]=String.valueOf(k);
}
for(j=1;j<count*2-1;j+=2)
{
if(count==2)
{
k1=Integer.valueOf(str[i][0]);
k2=Integer.valueOf(str[i][2]);
if((k1%k2)==0)
{
p=generateOperator();
str[i][j]=String.valueOf(p);
}
else
{
do {
p=generateOperator();
}while(p=='/');
str[i][j]=String.valueOf(p);
}
}
else
{
p=generateOperator();
str[i][j]=String.valueOf(p);
}
}
str[i][count*2-1]="=";
}
return str;
}

public static boolean ifRepeat(int number,int range,int count) { //判断题目是否重复
int i,j,k=1;
String[][] str=new String[number][count*2];
str=generateProblem(number,range,count);
for(i=0;i<number;i++)
{
for(j=0;j<i;j++)
{
if(str[j].equals(str[i]))
{
return false;

}
else continue;
}
if(k==0) break;
else continue;
}
return true;

}

public static void put(int number,int range,int count) { //输出
int i,j,k1,k2=0;
int x=0; //错题数为x+1
int count1=0,count2=0,count3=0,count4=0;
float len=0;

String[][] str=new String[number][count*2];


Scanner sc=new Scanner(System.in);
str=generateProblem(number,range,count);

for(i=0;i<number;i++)
{
if(count==2)
{
count1=Integer.valueOf(str[i][0]);
count2=Integer.valueOf(str[i][2]);
}

for(j=0;j<count*2;j++)
{
System.out.print(str[i][j]);
}
k1=sc.nextInt(); //做题者输入答案
for(j=1;j<count*2;j+=2)
{
if(count==2)
{
switch(str[i][j])
{
case "+":k2=count1+count2;break;
case "-":k2=count1-count2;break;
case "*":k2=count1*count2;break;
case "/":k2=count1/count2;break;
default:break;
}
if(k1==k2)
{
System.out.print("答案正确");
break;
}
else //存放进错题本
{
len++;
for(j=0;j<count*2;j++)
{
mis[x][j]=str[i][j];
}
x++;
System.out.print("答案错误");
break;
}
}

}
System.out.println();
}

System.out.println("您本次答题的错误率为"+(len/number)*100+"%");
System.out.println("您是否需要再做一套题:1.是,2.否");
int s1=sc.nextInt();
if(s1==1)
{
put(number,range,count);
}
System.out.println("您是否针对错题本练习一遍:1.是,2.否");
int s2=sc.nextInt();
if(s2==1)
{
putMisGrade2(number,range,count,x);
}
}

public static void putMisGrade2(int number,int range,int count,int x) //2年级的错题本输出
{
int count1=0, count2=0,k1,k2=0,i,j;
Scanner sc=new Scanner(System.in);
for(i=0;i<x;i++)
{
if(count==2)
{
count1=Integer.valueOf(mis[i][0]);
count2=Integer.valueOf(mis[i][2]);
}

for(j=0;j<count*2;j++)
{
System.out.print(mis[i][j]);
}
k1=sc.nextInt(); //做题者输入答案
for(j=1;j<count*2;j+=2)
{
if(count==2)
{
switch(mis[i][j])
{
case "+":k2=count1+count2;break;
case "-":k2=count1-count2;break;
case "*":k2=count1*count2;break;
case "/":k2=count1/count2;break;
}
if(k1==k2)
System.out.println("答案正确");
}
}
}
}

public static void main(String[] args)
{
int number,range,count=2; //count 为操作数的个数
Scanner sc=new Scanner(System.in);

System.out.println("小学二年级计算题");

System.out.println("请输入您所需要的题目个数:");
number=sc.nextInt();

System.out.println("请输入您需要的操作题的范围:");
range=sc.nextInt();

String[][] str=new String[number][count*2];
str=generateProblem(number,range,count);
put(number,range,count);
}

}