欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

四则运算缓冲流

程序员文章站 2022-05-06 11:37:12
...
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个随机题目,对于将题目写入文本里和读取文本内容不知怎么做,经过查阅一些资料和同学在讲台上展示的源代码,勉强符合老师的要求。对于这方面要加紧这方面的学习了。