四则运算缓冲流

package 四则运算;
import java.io.*;
import java.util.Random;
import java.util.Scanner;
public class szys {
	public static void main (String[] args)throws IOException,ClassNotFoundException{
	File f = new File("D:\\","Test.txt");
	f.createNewFile();
	FileOutputStream fis = new FileOutputStream(f);
	OutputStreamWriter scan1 = new OutputStreamWriter(fis);
Scanner sc=new Scanner(System.in);
int[]answer=new int[100];
for(int i=0;i<100;i++) {
	
Random r = new Random();
int a = r.nextInt(100);
int b =r.nextInt(100);
int s=r.nextInt(4)+1;
String a2="";
FileOutputStream scan11;
switch(s){
case 1:a2=""+a+"+"+b+"=?";answer[i] =a+b;break;
case 2:a2=""+a+"-"+b+"=?";answer[i] =a-b;break;
case 3:a2=""+a+"*"+b+"=?";answer[i]=a*b;break;
case 4:if(b==0) {
	a2=""+a+"+"+b+"=?";answer[i] =a+b;break;
}else if(a%b==0) {
	a2=""+a+"/"+b+"=?";answer[i]=a/b;break;
}else {
	
}a2=""+a+"-"+b+"=?";answer[i] =a-b;break;
}
scan1.write(a2);
scan1.write("请输入答案\r\n");

}
scan1.close();



File file = new File("D:\\Test.txt");
int i=0;

int score = 0;
if(file.exists()){  
    try {  
        FileReader fileReader = new FileReader(file);  
        BufferedReader br = new BufferedReader(fileReader);  
        String lineContent = null;  
        while((lineContent = br.readLine())!=null){  
            System.out.println(lineContent); 
           
            Scanner scan = new Scanner(System.in);
            int a1=scan.nextInt();
            
            if(a1==answer[i]) {
            	System.out.println("回答正确!");
            	score++;
            }
            else
            	{System.out.println("回答错误! 答案是"+answer[i]);}
             
            i++;
        }  
        br.close();  
        fileReader.close();  
        
    } catch (FileNotFoundException e1) {  
        System.out.println("no this file");//文件不存在抛异常  
        e1.printStackTrace();  
    } catch (IOException e1) {  
        System.out.println("io exception");  
        e1.printStackTrace();
        
    } 
} System.out.println("分数为:"+score);
	
}




}

  

总结:

首先做这个的时候一开始只能生成100个随机题目,对于将题目写入文本里和读取文本内容不知怎么做,经过查阅一些资料和同学在讲台上展示的源代码,勉强符合老师的要求。对于这方面要加紧这方面的学习了。

posted @ 2018-11-15 22:27  张利杰j  阅读(117)  评论(0编辑  收藏  举报