从文件中调取数据,生成随机题目,同时对答案进行验证。
一:文件操作的代码
1 // 从文件中读取数据 2 private static double[] getFile(String pathName) throws Exception { 3 // 【1】先创建一个File的实体对象 4 File file = new File(pathName); 5 if (!file.exists()) 6 throw new RuntimeException("找不到文件!"); 7 // 【2】加载BUfferedReader流 8 BufferedReader br = new BufferedReader(new FileReader(file)); 9 String str; 10 11 // 【3】一行一行读取 12 while ((str = br.readLine()) != null) { 13 14 int s = 0; 15 // 文件中数据的分割我用的是‘,’具体根据自己的情况调用下面的split()函数 16 String[] arr = str.split(","); 17 18 dArrs = new double[arr.length]; 19 for (int i = 0; i < arr.length; i++) { 20 String string = arr[i]; 21 int parseInt = Integer.parseInt(string); 22 dArrs[i] = parseInt; 23 24 } 25 } 26 return dArrs; 27 }
二:主函数代码:
public static void main(String[] args) { // 读取文件中的数据 try { double[] file = getFile("数据结构第二阶段.txt"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(); // 将数据存到文件中 try { toFile("inputFile.txt"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } // 调用出题函数进行出题 WQ(); // 下面由用户输入结果 System.out.println("请依次输入你的答案:"); Scanner scanner = new Scanner(System.in); String nextLine = scanner.nextLine(); String[] temp = nextLine.split(" "); Double[] Wresult = new Double[(int) sumOfQ]; for (int i = 0; i < temp.length; i++) { Double wResult = Double.parseDouble(temp[i]); Wresult[i] = wResult; } String a = compare(Wresult); }
三:用来出题的代码:wq()
// 用来出题的方法 public static void WQ() { // 下面开始主要功能的实现 System.out.print("请输入您要答题的数量:"); Scanner sc = new Scanner(System.in); sumOfQ = sc.nextInt(); System.out.println("正在为您生成题目请稍后。。。。。。。。。。。。"); Thread thread = new Thread(); try { thread.sleep(1000); thread.start(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } results = new double[(int) sumOfQ]; yunsuans = new String[] { "+", "—", "*", "/" }; for (int i = 0; i < sumOfQ; i++) { String yunsuan = yunsuans[(int) (yunsuans.length * Math.random())]; double firstNumber = dArrs[(int) (dArrs.length * Math.random())]; double secondNumber = dArrs[(int) (dArrs.length * Math.random())]; System.out.println((i + 1) + ", " + (int) firstNumber + yunsuan + (int) secondNumber + "="); switch (yunsuan) { case "+": results[i] = firstNumber + secondNumber; break; case "—": results[i] = firstNumber - secondNumber; break; case "*": results[i] = firstNumber * secondNumber; break; case "/": results[i] = firstNumber / secondNumber; break; } } System.out.println("答案请保留结果小数点后两位!!!"); /* * for(int i=0;i<results.length;i++) { System.out.print(results[i]+" "); } */ }
四:答案验证的代码:compare()
1 // 用来对比用户的答案和标准答案之间的方法 2 //参数为用户输入的答案数组 3 public static String compare(Double[] a) { 4 double t = 0; 5 //将答错的题的序号保存到list中方便调用 6 List<Integer> falseQ = new ArrayList<Integer>(); 7 for (int i = 0; i < results.length; i++) { 8 if (a[i] == DF_00(results[i])) 9 t++; 10 else { 11 12 falseQ.add(Integer.valueOf(i + 1)); 13 } 14 } 15 16 if (falseQ.isEmpty()) { 17 System.out.println("恭喜你,答对了所有题目!"); 18 } else { 19 System.out.print("答题完成,存在问题的序号为:"); 20 for (int i = 0; i < falseQ.size(); i++) { 21 System.out.print(falseQ.get(i) + " "); 22 } 23 } 24 System.out.println(); 25 System.out.println("你的正答率为:" + (DF_00(Double.valueOf(t / sumOfQ * 100))) + "%"); 26 27 return String.valueOf(DF_00(Double.valueOf(t / sumOfQ * 100)))+"%"; 28 }
五:进行数据规范(保留小数点后两位)的代码:DF_00():
// 用来将无尽小数和位数较多的小数转化为2位小数 public static double DF_00(Double b) { //调用DecimalForamt类的实例对象 对数据进行位数规范, //因为规范完后的数据我还要进行操作,我又将它进行强制转化 DecimalFormat dFormat = new DecimalFormat("#.00"); return Double.parseDouble(dFormat.format(b)); }