今天又来一次课堂测试, 这次测试让我对文件流的了解又加深了好多好多, 这次就像升华一般, 虽然和其他人比还有区别, 但与以前的我大不一样了.
- import java.io.*;
- import java.util.*;
- import org.omg.Messaging.SyncScopeHelper;
- public class Creat {
- int wrongNum=0;
- static File file=new File("answ.txt");
- static FileWriter fw=null;
- static FileReader fr = null;
- int[] answ = new int[31];
- String[] timu = new String[31];
- // 题目创建并存储
- public void creat() throws IOException {
- for (int i = 0; i < 3; i++) {
- int choice = (int) (Math.random() * 5);
- switch (choice) {
- // +
- case 0:
- doAdd(i + 1);
- break;
- // -
- case 1:
- doReduce(i + 1);
- break;
- //*
- case 2:
- doMul(i + 1);
- break;
- // /
- case 3:
- doDivide(i + 1);
- break;
- }
- }
- }
- // /
- private void doDivide(int i) throws IOException {
- // TODO Auto-generated method stub
- System.out.print("第" + i + "次为:");
- int one = (int) (Math.random() * 10);
- int two = (int) (Math.random() * 10);
- while (two == 0) {
- two = (int) (Math.random() * 10);
- }
- answ[i] = one / two;
- write(one,two,"/");
- System.out.println("答案为" + answ[i]);
- System.out.println(one + "/" + two + "=" + "\n\n");
- }
- //*
- private void doMul(int i) throws IOException {
- // TODO Auto-generated method stub
- System.out.print("第" + i + "次为:");
- int one = (int) (Math.random() * 10);
- int two = (int) (Math.random() * 10);
- answ[i] = one * two;
- String numOne = String.valueOf(one);
- String numTwo = String.valueOf(two);
- timu[i] = numOne + numTwo;
- write(one,two,"*");
- System.out.println("答案为" + answ[i]);
- System.out.println(one + "*" + two + "=" + "\n\n");
- }
- // -
- private void doReduce(int i) throws IOException {
- // TODO Auto-generated method stub
- System.out.print("第" + i + "次为:");
- int one = (int) (Math.random() * 10);
- int two = (int) (Math.random() * 10);
- answ[i] = one - two;
- String numOne = String.valueOf(one);
- String numTwo = String.valueOf(two);
- timu[i] = numOne + numTwo;
- write(one,two,"-");
- System.out.println("答案为" + answ[i]);
- System.out.println(one + "-" + two + "=" + "\n\n");
- }
- // +
- private void doAdd(int i) throws IOException {
- // TODO Auto-generated method stub
- System.out.print("第" + i + "次为:");
- int one = (int) (Math.random() * 10);
- int two = (int) (Math.random() * 10);
- answ[i] = one + two;
- String numOne = String.valueOf(one);
- String numTwo = String.valueOf(two);
- timu[i] = numOne + numTwo;
- write(one,two,"+");
- System.out.println("答案为" + answ[i]);
- System.out.println(one + "+" + two + "=" + "\n\n");
- }
- // 存储写入
- private void write(int one,int two,String f ) throws IOException {
- // TODO Auto-generated method stub
- fw=new FileWriter(file,true);
- BufferedWriter bw=new BufferedWriter(fw);
- bw.write(one+f+two+"=");
- bw.newLine();// 表示换行
- bw.write("*");
- bw.newLine();// 表示换行
- bw.flush();// 清空缓冲区
- bw.close();// 关闭输出流
- }
- // 测试判断区
- public void judge() throws IOException {
- // 首先, 读文件
- Scanner sc=new Scanner(System.in);
- BufferedReader br = null;
- fr = new FileReader(file);
- br = new BufferedReader(fr);
- String s = null;
- int i=0;
- while ((s = br.readLine())!=null) {
- if (s.equals("*")) {
- System.out.println("请输入答案:");
- @SuppressWarnings("unused")
- int a = sc.nextInt();
- if(a==answ[i]) {
- }
- else
- wrongNum++;
- } else {
- System.out.println(s);
- }
- }
- fr.close();
- System.out.println("一共错了"+wrongNum+"个!");
- }
- }
- import java.io.IOException;
- public class Test {
- public static void main(String[] args) throws IOException {
- // TODO Auto-generated method stub
- Test t=new Test();
- t.comeTrue();
- }
- private void comeTrue() throws IOException {
- // TODO Auto-generated method stub
- Creat c=new Creat();
- c.creat();
- c.judge();
- }
- }
来源: http://www.bubuko.com/infodetail-2848722.html