每日日报之一周总结
本周四则运算各项功能已全部完成。
源码如下:
package homework; import java.util.*; import java.util.Scanner; import javax.script.*; import java.io.File; import java.io.InputStreamReader; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileInputStream; import java.io.FileWriter; import java.io.IOException; import java.io.*; public class math { private static int n,min,max,c,cc,kh=0,xz,zts=0,zcts=0;//题目数,数值范围(最小及最大),操作数个数,乘除,括号,选择菜单,总题数,总错题数 private static char[] d={'+','-','*','/'};//运算符 private static Scanner sc=new Scanner(System.in); public static void main(String args[]){DZ();}//菜单 public static void DZ()//菜单 { System.out.println("请选择:"); System.out.println(" 1.小学二年级口算题"); System.out.println(" 2.小学三年级口算题"); System.out.println(" 3.小学四年级口算题"); System.out.println(" 4.错题本"); xz=sc.nextInt(); if(xz==4) {CTB();}//错题本 else { System.out.println( "请输入题目总数:" ); n=sc.nextInt(); System.out.println("请输入数值的范围."); min=sc.nextInt(); max=sc.nextInt(); switch(xz) { case 1: while(max>100) { System.out.println("小学二年级操作数范围不超过100,请重新输入最大值:"); max=sc.nextInt(); } c=1; break; case 2: while(max>1000) { System.out.println("小学三年级操作数范围不超过1000,请重新输入最大值:"); max=sc.nextInt(); } System.out.println( "输入操作数个数:" ); c=sc.nextInt()-1; while(c>3) { System.out.println("小学三年级操作数不超过4个,请重新输入操作数个数:"); c=sc.nextInt()-1; } break; case 3: System.out.println( "输入操作数个数:" ); c=sc.nextInt()-1; while(c>4) { System.out.println("小学四年级操作数不超过5个,请重新输入操作数个数:"); c=sc.nextInt()-1; } System.out.println( "是否有括号?是请输入1,否请输入0:" ); kh=sc.nextInt(); break; } System.out.println( "是否有乘除法?是请输入1,否请输入0:" ); cc=sc.nextInt(); TM();//题目 } } public static String Tm()//每题 { Random r=new Random(); int sz; String ch=null; double a=r.nextInt(max)+min; while(a>max){a=r.nextInt(max)+min;} if(xz==1) { int b; if(cc==0) {sz=r.nextInt(2);} else{sz=r.nextInt(4);}//乘除法 b=r.nextInt(max)+min; while(b>max){b=r.nextInt(max)+min;} if(d[sz]=='/') { if(b==0){while(b==0||b>max) {b=r.nextInt(max)+min;}}//防止0为被除数 if(a%b!=0) { while(a%b!=0||a>max||b>max) { a=r.nextInt(max)+min; b=r.nextInt(max)+min; } } } if(d[sz]=='-'&&b>a){while(b>a||b>max) {b=r.nextInt(max)+min;}} ch=String.valueOf(a); ch+=String.valueOf(d[sz]); ch+=String.valueOf(b); } else { double[] b=new double[c];//操作数个数及数值范围 int k=-1,h=-1,z;//左括号,右括号 if(kh==1)//括号 { z=r.nextInt(2); if(z==1)//随机增加括号 { k=r.nextInt(c-1); if(k==0)//括号由开头起 { ch=String.valueOf('('); ch+=String.valueOf(a); h=r.nextInt(c-1)+k+1; while(h>c){h=r.nextInt(c-1)+k+1;} } if(k>0)//括号在式中 { ch=String.valueOf(a); h=r.nextInt(c)+k+1; while(h>c){h=r.nextInt(c)+k+1;} } } else{ch=String.valueOf(a);} } else{ch=String.valueOf(a);} for(int i=0;i<c;i++) { if(cc==0) {sz=r.nextInt(2);} else {sz=r.nextInt(4);}//乘除法 ch+=String.valueOf(d[sz]); if(k>0&&i==k-1){ch+=String.valueOf('(');} b[i]=r.nextInt(max)+min; while(b[i]>max){b[i]=r.nextInt(max)+min;} if(d[sz]=='/'&&b[i]==0){while(b[i]==0||b[i]>max) {b[i]=r.nextInt(max)+min;}}//防止0为被除数 ch+=String.valueOf(b[i]); if(i==h-1){ch+=String.valueOf(')');}//补“)” } } return ch;//以字符串形式定义算式 } public static void TM()//总题 { zts+=n; int j=0; String[] s=new String[n]; while(j<n)//制作题目 { s[j]=Tm(); if(PC(j,s)==0)//是否重复 { System.out.println((j+1)+"、"+s[j]+"="); j++; } } ScriptEngine se=new ScriptEngineManager().getEngineByName("JavaScript"); double[] sum=new double[n]; for(int i=0;i<n;i++) { try{sum[i]=(Double)se.eval(s[i]);} catch(ScriptException e){e.printStackTrace();} }//字符串转算式计算结果 DT(s,sum);//答题 } public static int PC(int j,String[] s)//判重 { for(int i=0;i<j;i++){if(s[i].equals(s[j])) {return 1;}} return 0; } public static void DT(String[] s,double[] sum)//答题 { int mint,yd=0,w=0,yn=0;//时间,已答题目,错题数,yes or no double[] da=new double[n];//回答 int[] cwcs=new int[n]; double[] Da=new double[n]; double[] Sum=new double[n]; String[] Cw=new String[n];//错误题号 System.out.println( "请设置答题时间(分钟)"); mint=sc.nextInt(); System.out.println( "开始答题 !"); long startTime=System.currentTimeMillis(); while((System.currentTimeMillis()-startTime)/1000<=mint*60) { da[yd]=sc.nextDouble(); yd++; if(yd==n){break;} } if(yd!=n){System.out.println( "时间到!答题结束!");} for(int i=0;i<n;i++) { if(da[i]!=sum[i])//判断答题对错 { Cw[w]=s[i]; Da[w]=da[i]; Sum[w]=sum[i]; cwcs[w]=1; w++; zcts++; } } try { File f=new File("CTB.txt"); try(FileWriter writer = new FileWriter(f,true);BufferedWriter out = new BufferedWriter(writer);) { for(int i=0;i<w;i++){out.write(Cw[i]+"="+Sum[i]+",共做错了"+cwcs[i]+"次。\r\n");} out.flush(); out.close(); } }catch (IOException e) {e.printStackTrace();} double cw=(double)(n-w)*100/n;//正确率 System.out.println( "你的正确率为 :" +cw+"%"); System.out.println( "你的错题如下 :");//错题 for(int i=0;i<w;i++){System.out.println((i+1)+"、"+Cw[i]+"="+Da[i]+" X"+" 正确答案:"+Sum[i]);} do { System.out.println("请选择:"); System.out.println(" 1.再做一套题"); System.out.println(" 2.错题重练"); System.out.println(" 3.退出"); yn=sc.nextInt(); switch(yn) { case 1: TM(); break; case 2: for(int i=0;i<w;i++) { System.out.println((i+1)+"、"+Cw[i]+"="); da[i]=sc.nextDouble(); if(da[i]!=Sum[i]) { cwcs[i]++; System.out.println((i+1)+"、X 正确答案为:"+Sum[i]); try { File f=new File("CTB.txt"); BufferedReader in=new BufferedReader(new FileReader(f)); PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f,true))); String line; while ((line = in.readLine()) != null) { String[] tokens = line.split(","); if(tokens[0].equals(Cw[i]+"="+Sum[i])) { out.println(line.replace(tokens[1], "共做错了"+cwcs[i]+"次。")); } } in.close(); out.close(); }catch (IOException e) {e.printStackTrace();} } } break; case 3: System.out.println( "你今天的正确率为 :" +(zts-zcts)*100/zts+"%"+"\r\n"); CTB(); break; } }while(yn!=3); } public static void CTB()//错题本 { System.out.println(" 错题本 "); try { File f=new File("CTB.txt"); InputStreamReader reader = new InputStreamReader(new FileInputStream(f)); BufferedReader br = new BufferedReader(reader); String line; while ((line = br.readLine()) != null){System.out.println(line);} br.close(); }catch (IOException e) {e.printStackTrace();} } }
功能记录:
定制式题、四则运算及括号添加、年级的限定、错题记录及重练、多套试题的限时答题、错题本等。
问题无(主要是懒。。。)