日期:2018.11.15
博客期:024
星期四
今天上课老师又出题了,这次的题目是去利用文件的读写去完成文件的读写答题!那为什么我没有第一时间做出来呢?原因我分析了以下几点:
1、我第一时间对题意没有准确的审视,我先做出来的是直接利用文件读写同时完成!呃~简单来说就是在题目判断的同时向文件里写入内容。等发现分两步做的时候,已经过了第一时间了。
2、我的代码库确实很鸡肋!虽然存着上一回的代码,但是我发现还需要改一些问题,那就是...文件的读写相关方法!在之前的Question类里,我就只有leftNumber和operator、rightNumber、id三个数据成员,不能实现多个数的运算!
3、我的态度很敷衍,看到自己在第一时间快结束的时候还没有做完,心态就会感觉爆炸一样!后面做题也很难受!我觉得以后做Java软件还是需要放松一点...
4、我对于文件的读写都是通过固定的模式来实现的,其他的方法我也没有去做更深一步的研究。看到要用BufferedReader做,自己的Scanner配套PrintWriter的模式用不上,自然做起来也就相等难受!
5、老师说不要用web界面的时候,自己是很难受的,毕竟最近一直在学习Java web方面的知识,曾经的Java知识也没有去管,可能对Java控制台界面有点陌生了?
原因看起来很好笑,但却是很实际的。我觉得我能从这次的练习学到很多生活、学习方法,以及改变了我的心态!所以,我还是得到很多的!
代码:
1 package src; 2 3 import java.io.File; 4 import java.io.IOException; 5 import java.io.PrintWriter; 6 7 public class Question { 8 //题目号 9 private int id; 10 //左操作数 11 private int leftNumber; 12 //右操作数 13 private int rightNumber; 14 //操作符 15 private char operator; 16 //随即构造器 17 private long temp = System.currentTimeMillis(); 18 //最大值 19 private int MaxValue = 1000; 20 //重设方法 21 public void reset(){ 22 renew(); 23 int cx = (int)(temp)%4; 24 renew(); 25 char x = '\0'; 26 switch(cx) 27 { 28 case 0:x='+';break; 29 case 1:x='-';break; 30 case 2:x='*';break; 31 case 3:x='/';break; 32 } 33 reset(x); 34 } 35 public void reset(char x){ 36 leftNumber = (int)temp%MaxValue+1; 37 renew(); 38 rightNumber = (int)temp%MaxValue+1; 39 renew(); 40 if(x=='+') 41 { 42 operator='+'; 43 if(leftNumber+rightNumber>=MaxValue||rightNumber<MaxValue/MaxValue/10||leftNumber<MaxValue/100) 44 reset('+'); 45 } 46 else if(x=='-') 47 { 48 operator='-'; 49 if(leftNumber<rightNumber||rightNumber<MaxValue/10) 50 reset('-'); 51 } 52 else if(x=='*') 53 { 54 operator='*'; 55 if(leftNumber>MaxValue/10&&rightNumber>MaxValue/10||rightNumber==1||leftNumber==1) 56 reset('*'); 57 } 58 else if(x=='/') 59 { 60 operator='/'; 61 if(leftNumber<rightNumber||leftNumber%rightNumber!=0||rightNumber==1) 62 reset('/'); 63 } 64 } 65 //set、get方法 66 public int getId() { 67 return id; 68 } 69 public void setId(int id) { 70 this.id = id; 71 } 72 public int getLeftNumber() { 73 return leftNumber; 74 } 75 public void setLeftNumber(int leftNumber) { 76 this.leftNumber = leftNumber; 77 } 78 public int getRightNumber() { 79 return rightNumber; 80 } 81 public void setRightNumber(int rightNumber) { 82 this.rightNumber = rightNumber; 83 } 84 public char getOperator() { 85 return operator; 86 } 87 public String getOperator(boolean x) { 88 return (operator+""); 89 } 90 public void setOperator(char operator) { 91 this.operator = operator; 92 } 93 //得到结果 94 public int answer(){ 95 if(operator=='+') 96 return leftNumber+rightNumber; 97 else if(operator=='-') 98 return leftNumber-rightNumber; 99 else if(operator=='*') 100 return leftNumber*rightNumber; 101 else if(operator=='/') 102 return leftNumber/rightNumber; 103 else 104 return 0; 105 } 106 //输出数据 107 public void print(){ 108 System.out.print(id+"、 "+leftNumber+" "+operator+" "+rightNumber+" = "); 109 } 110 public void println(){ 111 System.out.println(id+"、 "+leftNumber+" "+operator+" "+rightNumber+" = "); 112 } 113 //输出到文件 114 public void PrintToFile(File f) throws IOException{ 115 PrintToFile(1,f,false,false,false); 116 } 117 public void PrintToFile(int number,File f) throws IOException{ 118 PrintToFile(number,f,false,false,false); 119 } 120 public void PrintToFile(int number,File f,boolean hasID) throws IOException{ 121 PrintToFile(number,f,hasID,false,false); 122 } 123 public void PrintToFile(int number,File f,boolean hasID,boolean hasEquals) throws IOException{ 124 PrintToFile(number,f,hasID,hasEquals,false); 125 } 126 public void PrintToFile(int number,File f,boolean hasID,boolean hasEquals,boolean hasAnswer) throws IOException{ 127 if(f.exists()) 128 f.delete(); 129 f.createNewFile(); 130 PrintWriter pw = new PrintWriter(f); 131 for(int i=1;i<=number;++i) 132 { 133 if(hasID) 134 pw.print(id+"、 "); 135 pw.print(leftNumber+" "+operator+" "+rightNumber); 136 if(hasEquals) 137 pw.print(" = "); 138 if(hasAnswer) 139 pw.print(answer()); 140 pw.println(); 141 } 142 pw.close(); 143 } 144 //更新随机数 145 public void renew(){ 146 temp = ((temp*16807)%Integer.MAX_VALUE); 147 } 148 //构造方法 149 public Question(){ 150 id = 1; 151 reset(); 152 try { 153 Thread.sleep(2); 154 } catch (InterruptedException e) { 155 // TODO 自动生成的 catch 块 156 e.printStackTrace(); 157 } 158 } 159 public Question(int ID){ 160 id = ID; 161 reset(); 162 try { 163 Thread.sleep(2); 164 } catch (InterruptedException e) { 165 // TODO 自动生成的 catch 块 166 e.printStackTrace(); 167 } 168 } 169 public Question(int l,char o,int r){ 170 id = 1; 171 leftNumber = l; 172 operator = o; 173 rightNumber = r; 174 } 175 public Question(int ID,int l,char o,int r){ 176 id = ID; 177 leftNumber = l; 178 operator = o; 179 rightNumber = r; 180 } 181 public Question(int ID,char []operator,int length,int MaxValue){ 182 try { 183 Thread.sleep(2); 184 } catch (InterruptedException e) { 185 // TODO 自动生成的 catch 块 186 e.printStackTrace(); 187 } 188 } 189 //主方法 190 public static void main(String[] args) { 191 Question [] p = new Question [30]; 192 for(int i=0;i<30;++i) 193 { 194 p[i] = new Question(i+1); 195 p[i].println(); 196 } 197 } 198 }
1 package src; 2 3 import java.io.BufferedReader; 4 import java.io.BufferedWriter; 5 import java.io.File; 6 import java.io.FileWriter; 7 import java.io.IOException; 8 import java.io.InputStreamReader; 9 import java.util.Scanner; 10 11 public class Test { 12 public void test1(int Add) throws IOException 13 { 14 File f = new File("question/Test.txt"); 15 if(f.exists()) 16 f.delete(); 17 f.createNewFile(); 18 19 BufferedWriter bw = new BufferedWriter(new FileWriter(f)); 20 for(int i=1;i<=Add;++i) 21 { 22 Question q = new Question(i); 23 bw.write(q.getId()+"、 "+q.getLeftNumber()+" "+q.getOperator()+" "+q.getRightNumber()+" = "); 24 bw.newLine(); 25 bw.write(Integer.toString(q.answer())); 26 bw.newLine(); 27 bw.write("*****************************"); 28 bw.newLine(); 29 } 30 bw.close(); 31 } 32 public int test2(int Add) throws IOException{ 33 BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 34 Scanner sc = new Scanner(new File("question/Test.txt")); 35 int score = 0; 36 for(int i=1;i<=Add;++i) 37 { 38 Question q = new Question(i); 39 sc.next(); 40 int leftNumber = sc.nextInt(); 41 char operator = sc.next().charAt(0); 42 int rightNumber = sc.nextInt(); 43 q.setLeftNumber(leftNumber); 44 q.setOperator(operator); 45 q.setRightNumber(rightNumber); 46 q.println(); 47 sc.nextLine(); 48 sc.nextLine(); 49 sc.nextLine(); 50 boolean temp = false; 51 int answer = 0; 52 while(!temp) 53 { 54 String str = br.readLine(); 55 int l = str.length(); 56 char []c = str.toCharArray(); 57 for(int j = 0;j<l;++j) 58 if(!(c[j]>='0'&&c[j]<='9')) 59 { 60 temp = false; 61 continue; 62 } 63 temp = true; 64 answer = Integer.parseInt(str); 65 } 66 if(answer==q.answer()) 67 { 68 System.out.println("结果正确!"); 69 ++score; 70 } 71 else 72 { 73 System.out.println("结果错误!"); 74 } 75 System.out.println("*****************************"); 76 } 77 return score; 78 } 79 public static void main(String[] args) throws IOException { 80 int Add = 3; 81 Test t = new Test(); 82 t.test1(Add); 83 int score = t.test2(Add); 84 System.out.println("您共回答了"+Add+"道题,正确"+score+"道,错误"+(Add-score)+"道"); 85 } 86 }
截图: