课堂小测

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("f:\\","szys.txt");
        f.createNewFile();
        FileOutputStream fis = new FileOutputStream(f);
        OutputStreamWriter os = new OutputStreamWriter(fis);
        double[] answer = new double [100];
        for(int i = 0 ; i < 100;i++) {
            double a = new Random().nextInt(10);
            double b = new Random().nextInt(10);
            int suijifuhao = new Random().nextInt(4);
            String fuhao=" ";
        switch(suijifuhao) {
            case(0): 
                 {fuhao = "+";answer[i]= (a+b);   break;}
            case(1): 
                 {fuhao = "-";answer[i]= (a-b);   break;}
            case(2): 
                 {fuhao ="×";answer[i]= (a*b);   break;}
            case(3): 
                       if(b==0) { fuhao = "+";answer[i]= (a+b);   break;}
                        else
                           {fuhao = "/";answer[i]= (a/b);break;}
                }      
        os.write(a+fuhao+b+"=\r\n");
        }
        os.close();

        
        
        
        File file = new File("f:\\szys.txt");
        
        int n=0;
        int score = 0;
        if(file.exists()){  
 
                FileReader fileReader = new FileReader(file);  
                BufferedReader br = new BufferedReader(fileReader);  
                String lineContent = null;  
                while((lineContent = br.readLine())!=null){  
                    System.out.println(lineContent); 
                   
                    Scanner sc = new Scanner(System.in);
                   double a=sc.nextDouble();
                    
                    if(a==answer[n]) {
                        System.out.println("回答正确!");
                        score++;
                    }
                    else
                        {System.out.println("回答错误! 答案是"+answer[n]);}
                    n++;
                }  
                br.close();  
                fileReader.close();  
            } 
         System.out.println("分数为:"+score);
            
    }
}

总结:在这次小测中,最开始就是套用之前的模板,结果发现完全不行。然后还是老老实实写的四则运算。在文件导入的时候在网上查找了一定儿资料.fileoutputstream可以进行文本的创建和输入。这个没用多少时间。但是在后面的读取文本的时候。核对答案的时候。开始的时候就是核对不上。答案是错误的。就算输入正确的还是显示不对。最后找别人给我看了看。他把我的写入文件和答案的存入分开了。就可以了。

猜你喜欢

转载自www.cnblogs.com/birdmmxx/p/9965311.html