四则运算+文件
Posted daisy99lijing
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了四则运算+文件相关的知识,希望对你有一定的参考价值。
1 package Forth; 2 import java.io.*; 3 import java.util.*; 4 public class Main1 { 5 6 public static void main(String[] args) throws IOException { 7 // TODO 自动生成的方法存根 8 try{ 9 List<Question>questionBase=new ArrayList<Question>();//题库 10 int r=4;//操作数范围 11 int n=10;//题目个数 12 Question q=new Question(1); 13 questionBase.add(q); 14 while(questionBase.size()<n||Question.isRepeated(questionBase)){ 15 q=new Question(r); 16 questionBase.add(q);//一个个向数据库添加内容 17 } 18 19 //生成题目文件,生成写入文件 20 BufferedWriter bw1=new BufferedWriter(new FileWriter("C:\\Users\\ASUS\\Desktop\\四则运算\\Questions.txt")); 21 22 String e="C:\\Users\\ASUS\\Desktop\\四则运算\\Questions.txt"; 23 24 System.out.println("***5**"); 25 for(int i=0;i<questionBase.size();i++){ 26 String s1=i+1+"."+questionBase.get(i)+" "; 27 bw1.write(s1); 28 } 29 bw1.flush();//把流的内容压倒文件中显示出来 30 bw1.close(); 31 //br1.close(); 32 33 //生成答案文件Answers.txt 34 BufferedWriter bw2=new BufferedWriter(new FileWriter("C:\\Users\\ASUS\\Desktop\\四则运算\\Answers.txt")); 35 for(int i=0;i<questionBase.size();i++){ 36 String s=questionBase.get(i).result+"*"+" "; 37 bw2.write(s); 38 } 39 bw2.flush(); 40 bw2.close(); 41 System.out.println("请打开文件并在文件中输入答案:"); 42 System.out.println("如果作答完成,请及时保存。。。。并输入1"); 43 Scanner in=new Scanner(System.in); 44 int flag=in.nextInt(); 45 if(flag==1){ 46 //读答题文件 47 BufferedReader br1=new BufferedReader(new FileReader(e)); 48 List<String> exercises=new ArrayList<String>(); 49 List<String> answers=new ArrayList<String>(); 50 String str=""; 51 str=br1.readLine(); 52 for(int i=0;null!=str;i++) 53 exercises.add(str.replace(i+1+". "+questionBase.get(i),"")); 54 br1.close(); 55 String d="C:\\Users\\ASUS\\Desktop\\四则运算\\Answers.txt"; 56 //读取答案文件 57 BufferedReader br2=new BufferedReader(new FileReader(d)); 58 for(int i=0;null!=(str=br2.readLine());i++) 59 answers.add(str=br2.readLine()); 60 br2.close(); 61 String correct="",wrong=""; 62 List<Integer> corrects=new ArrayList<Integer>(); 63 List<Integer> wrongs=new ArrayList<Integer>(); 64 for(int i=0;i<answers.size();i++){ 65 if(exercises.get(i).equals(answers.get(i))) corrects.add(i+1); 66 else wrongs.add(i+1); 67 } 68 for(int i=0;i<corrects.size();i++) correct+=corrects.get(i)+","; 69 for(int i=0;i<wrongs.size();i++) wrong+=wrongs.get(i)+","; 70 if(corrects.size()>0) correct=correct.substring(0,correct.length()-1); 71 if(wrongs.size()>0) wrong=wrong.substring(0,wrong.length()-1); 72 73 74 75 System.out.println("*****"); 76 //对错题数量统计 77 String a=""; 78 for(int i=0;i<args.length;i++){ 79 if(args[i].equals("-e")) e=args[i+1]; 80 if(args[i].equals("-a")) a=args[i+1]; 81 } 82 //没有给出答题文件路径 83 if(e.equals("")){ 84 System.err.println("404: The exercises file is not found!"); 85 return; 86 } 87 //没有给出答案文件路径 88 if(a.equals("")){ 89 System.err.println("404: The answers file is not found!"); 90 return; 91 } 92 //生成分数文件Grade.txt 93 BufferedWriter bw=new BufferedWriter(new FileWriter("C:\\Users\\ASUS\\Desktop\\四则运算\\Grade.txt")); 94 bw.write("Correct:"+corrects.size()+"("+correct+") Wrong:"+wrongs.size()+"("+wrong+")"); 95 bw.flush(); 96 bw.close(); 97 } 98 } catch(FileNotFoundException e) { 99 e.printStackTrace(); 100 } 101 catch(IOException e) { 102 e.printStackTrace(); 103 } 104 105 } 106 }
3.5h
以上是关于四则运算+文件的主要内容,如果未能解决你的问题,请参考以下文章
获取 badarith,[erlang,'+',[error,0],[],同时使用 Erlang 片段在 TSUNG 中执行算术运算