9.16-17四则运算2
package temomomomo;
import java.util.Random;
import java.util.Scanner;
public class sizeyunsuan2 {
static Scanner in = new Scanner(System.in);//一定要写上static不然报错
static String[] operator1= new String[] {"+","-","x","÷"};
static String[] operator2=new String[] {"+","-"};
static String[] a = new String[10000];
static int[] daan=new int[10000];//正确答案
static int[] da=new int [10000];//输入答案
static int num,pri,opp,max,min;//题目数、打印方式、操作数、最大值、最小值
static String ch,kuohao;//是否需要乘除法、括号
// 页面:需求输入
public static void yemian() {
System.out.println("输入题目数量:");
num=in.nextInt();
System.out.println("打印方式:");
pri=in.nextInt();
System.out.println("操作数个数:");
opp=in.nextInt()-1;
System.out.println("是否有乘除法(Y/N):");
ch=in.next();
System.out.println("是否需要括号(Y/N):");
kuohao=in.next();
System.out.println("取值范围:");
min=in.nextInt();
max=in.nextInt();
}
// 生成随机数子方法,k=1数字,k=2操作2个,k=3操作4个,k=4括号随机数
public static int random(int k){
Random r=new Random();
if(k==1) {
int m=max-min+1;
int[] numbers=new int [m];
int j=min;
for(int i=0;i<m;i++) {
numbers[i]=j;
j++;
}
return numbers[r.nextInt(m)];
}
else if(k==2) {
return r.nextInt(2);
}
else if(k==3){
return r.nextInt(4);
}
else {
return r.nextInt(opp)+1;
}
}
// 生成并打印
public static void shengcheng() {
int n=0;
for(int i=0;i<num;i++) {
String str="";
int[] nnum=new int[opp+1];
if(kuohao.equals("Y")) {
int weizhi=random(4);
int find=0;
for(int k=0;k<opp;k++) {
if(weizhi-1==k) {
str+="(";
find=1;
if(ch.equals("N")==true) {
str+=String.valueOf(random(1))+operator2[random(2)];
}
else {
str+=String.valueOf(random(1))+operator2[random(2)];
}
}
else {
str+=String.valueOf(random(1));
if(find==1) {
str+=")";
find=0;
}
if(ch.equals("N")==true) {
str+=operator2[random(2)];
}
else {
str+=operator1[random(3)];
}
}
}
str+=String.valueOf(random(1));
if(find==1) {
str+=")";
}
str+="=";
}
else {
for(int k=0;k<opp;k++) {
if(ch.equals("N")==true) {
str+=String.valueOf(random(1))+operator2[random(2)];
}
else {
str+=String.valueOf(random(1))+operator1[random(3)];
}
}
str+=String.valueOf(random(1))+"=";
}
a[n]=str;
n++;
System.out.print(str);
if(n%pri==0)System.out.print("\n");
else System.out.print("\t");
}
}
// 判断是否有重复
public static void chongfu() {
int cout=0;
for(int i=0;i<num-1;i++) {
for(int j=i+1;j<num;j++) {
if(a[i].equals(a[j])==true) {
cout++;
}
}
}
System.out.println("题目重复数量:"+cout);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
yemian();
shengcheng();
chongfu();
}
}
不会:<记录一下
package temomomomo;
//import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Random;
import java.util.Scanner;
class sizeyunsuan3 {
static String[] opString=new String[] {"+","-"};
static Scanner in = new Scanner(System.in);
static int num,opnum;
static String maxString;//999999999999字符串
static int maxInt;//999999999个数
// static BigDecimal b;
static BigInteger b;
// static float bb;
static String[] timu=new String[10000];//存放题目下标法
static String[] daan=new String[10000];//存放正确答案,以String类型存储big转String下标法
static String[] shuru=new String[10000];//存储输入的答案下标法
static int[] cuowu1=new int[10000];//存入错误的下标
static int[] cuowu2=new int[10000];//存入错误次数下标法
static int err=0;//错题数量
public static void yemian() {
System.out.println("请输入题目数量:");
num=in.nextInt();
System.out.println("请输入操作数:");
opnum=in.nextInt();
System.out.println("请输入最大范围 :");
maxString=in.next();
b=new BigInteger(maxString);//
// bb=b.floatValue();//
maxInt=maxString.length();//没有用
}
public static void chuti() {
for(int i=0;i<num;i++) {
String str="";//记录题目
BigInteger sum = new BigInteger(64, new Random());//生成第一个随机数256 64:20位??
// BigDecimal sum=new BigDecimal(Math.random()*bb);
str+=String.valueOf(sum);//转
for(int j=0;j<opnum-1;j++) {
BigInteger a = new BigInteger(64, new Random());
// BigDecimal a=new BigDecimal(Math.random()*bb);
int fuhao=(int)(Math.random()*2);//0,1
switch(fuhao) {
case 0:str+="+";sum=sum.add(a);break;
case 1:str+="-";sum=sum.subtract(a);break;
}
str+=String.valueOf(a);
}
str+="=";
timu[i]=str;
daan[i]=String.valueOf(sum);
System.out.print(str);
shuru[i]=in.next();
if(daan[i].equals(shuru[i])==false) {
cuowu1[err]=i;//cun下标
cuowu2[cuowu1[err]]++;//存错误次数
err++;
}
}
System.out.println("正确率:"+(double)(num-err)/num*100+"%");
System.out.print("1.重做练习题 2.查看错题集");
int choose;
choose=in.nextInt();
switch(choose) {
case 1:chongzuo();break;
case 2:cuotiji();break;
}
}
public static void chongzuo() {
int k=0;
for(int i=0;i<err;i++) {
System.out.print(timu[cuowu1[i]]);
shuru[cuowu1[i]]=in.next();
if(daan[cuowu1[i]].equals(shuru[cuowu1[i]])==false) {
cuowu2[cuowu1[i]]++;//存错误次数
k++;
}
}
System.out.println("正确率:"+(double)(err-k)/err*100+"%");
cuotiji();
}
public static void cuotiji() {
// System.out.println("正确率:"+(double)(num-err)/num*100+"%");
for(int i=0;i<err;i++) {
System.out.println(timu[cuowu1[i]]+daan[cuowu1[i]]+"答错"+cuowu2[cuowu1[i]]+"次");
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
yemian();
chuti();
}
}