课堂小测

本文介绍了一个使用Java实现的四则运算题目生成及答案评分系统。系统能够生成包含加、减、乘、除四种运算的题目,并将题目写入文本文件。之后,系统读取文件中的题目,允许用户输入答案并进行评分。
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可以进行文本的创建和输入。这个没用多少时间。但是在后面的读取文本的时候。核对答案的时候。开始的时候就是核对不上。答案是错误的。就算输入正确的还是显示不对。最后找别人给我看了看。他把我的写入文件和答案的存入分开了。就可以了。

转载于:https://www.cnblogs.com/birdmmxx/p/9965311.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值