1.15学习总结
今天学习的主要内容还是编写二柱子的程序,对我来说最大的收获就是借助jdk帮助文档了解了一些方法的功能并实际运用到了程序的编写当中,练习了方法的构造和调用。项目做到这里还是有一些问题没有解决,就是我现在只能完成二年级题目要求的编写,也就是只有两个操作数的情况,多个操作数不知道怎么生成(阅读别人的代码也有一些困难)。接下来有时间的话打算用栈的思想来重写一下项目,可能会突破当前的难题。
代码:
package com.demo; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class Main { public static void main(String[] args) { Main Instance = new Main(); Scanner ac = new Scanner(System.in); System.out.println("输入四则运算的数量"); int n = Integer.parseInt(ac.nextLine()); System.out.println("输入范围"); int end = Integer.parseInt(ac.nextLine()); ArrayList<String> ans = new ArrayList<String>(); HashMap<String,Integer> wrongCnt = new HashMap<String, Integer>();//记录错题次数 HashMap<String,Integer> result = new HashMap<String, Integer>();//记录答案 //用于保存计算结果 for(int i = 0;i < n;i++){ StringBuffer tmp = new StringBuffer(); int x = (int)(Math.random()*n); tmp.append(x); int y = (int)(Math.random()*n); int op = (int)(Math.random()*4); int res = 0;//记录答案 switch (op){ case 0: tmp.append("+"); res=x+y; break; case 1: tmp.append("-"); res = x-y; break; case 2: tmp.append("*"); res = x*y; break; case 3: tmp.append("/"); if(y == 0) res = 0; else res = x/y; break; } tmp.append(y); result.put(tmp.toString(),res); ans.add(tmp.toString()); } char s = ' '; ArrayList<String> wrong = new ArrayList<String>(); while (s != 'q'){ System.out.println("***************输入a进入普通测试*********"); //System.out.println("***************输入b进入定时测试*********"); System.out.println("***************输入c进入错题测试*********"); System.out.println("***************输入d展示错题记录*********"); System.out.println("***************输入q退出*********"); String str = ac.nextLine(); s = str.charAt(0); switch (s){ case 'a': wrong = Instance.normalTest(ans,result,ac,wrongCnt); break; // case 'b': // wrong = Instance.TimeTest(ans,result,ac,wrongCnt); // break; case 'c': wrong = Instance.reTest(wrong,result,ac,wrongCnt); break; case 'd': Instance.WrongCnt(wrongCnt); break; case 'q': return; } } } /** * 定时测试 * @param test * @param result * @param ac * @param wrongCnt */ public ArrayList<String> TimeTest(ArrayList<String> test, HashMap<String,Integer> result, Scanner ac, HashMap<String,Integer> wrongCnt){ return null; } /** * 普通测试 * @param test 题目集 * @param result 结果集 * @return 返回错题集 */ public ArrayList<String> normalTest(ArrayList<String> test,HashMap<String,Integer> result,Scanner ac,HashMap<String,Integer> wrongCnt){ int n = test.size(); int right = 0; System.out.println("开始答题"); ArrayList<String> wrongAns = new ArrayList<String>(); for(String s:test){ System.out.println("题目是:"+s); System.out.print("请输入你的答案:"); int res = Integer.parseInt(ac.nextLine()); if(res == result.get(s)) right++; else wrongAns.add(s); } double radio = right/(double)n; System.out.println("正确率为:"+radio); System.out.println("错误的题目是:"); for(String s:wrongAns) { System.out.println(s); if(!wrongCnt.containsKey(s)){ wrongCnt.put(s,1); }else { int cnt = wrongCnt.get(s); wrongCnt.put(s,cnt+1); } } return wrongAns; } /** * 错题重做 * @param wrongTest 错题集 * @param result 结果 * @param ac * @param wrongCnt 错题次数记录 * @return */ public ArrayList<String> reTest(ArrayList<String> wrongTest,HashMap<String,Integer> result,Scanner ac,HashMap<String,Integer> wrongCnt){ ArrayList<String> wrong = new ArrayList<String>(); int sum = 0; for(String s:wrongTest){ System.out.println("错题是:"); System.out.println(s); System.out.println("你的答案是:"); int ans = Integer.parseInt(ac.nextLine()); if(ans != result.get(s)){ sum++; wrong.add(s); int cnt = wrongCnt.get(s); wrongCnt.put(s,cnt+1); } } System.out.println("错题错误: "+ sum+ "个"); return wrong; } /** * 用于打印错误题目的次数 * @param wrong */ public void WrongCnt(HashMap<String,Integer> wrong){ for(Map.Entry w:wrong.entrySet()){ String s = (String)w.getKey(); int cnt = (Integer)w.getValue(); System.out.println("错题"+s+"错了"+" "+cnt+ "次"); } } }