每日日報之一週總結
阿新 • • 發佈:2020-10-18
本週四則運算各項功能已全部完成。
原始碼如下:
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();} } }
功能記錄:
定製式題、四則運算及括號新增、年級的限定、錯題記錄及重練、多套試題的限時答題、錯題本等。
問題無(主要是懶。。。)