zoukankan      html  css  js  c++  java
  • 四则运算(文件存储)

    当堂测试,将四则运算随机生成100道题,存储在TXT文本中,同时在题目后面存储*作为分界符,实现答题判断对错并汇总功能。

    【设计思想】

      1.随机生成100道四则运算计算题

      2.将生成的100道题存储在.txt文件里

      3.读取txt文件,并作答,判断答案正误,输出汇总

      设计思想很简单,一些代码实现也不是很难,真正困扰我编程的是文件的输入流和输出流操作,BufferedReader和BufferedWriter,对于文件的操作差的太多,自己也不知道如何下手文件问题,最后还是在同学帮助下才弄懂一点点操作。空余时间还是应该多练一下。

    【源代码】为测试方便,将生成题目数改成10道

      1 package demo;
      2 
      3 import java.io.BufferedReader;
      4 import java.io.BufferedWriter;
      5 import java.io.FileReader;
      6 import java.io.FileWriter;
      7 import java.io.IOException;
      8 import java.util.Scanner;
      9 
     10 public class chuti {
     11 
     12     static String[] question = new String[100];
     13     static int[] answer = new int[100];
     14 
     15     public static int getRandom(int n, int m) {
     16         // 产生n->m的随机数
     17         return (int) (Math.random() * (m - n) + n);
     18     }
     19 
     20     public static char getCharRandom() {
     21         // 随机产生四种运算符
     22         char sign = 0;
     23         int Sn;
     24         Sn = getRandom(1, 5);
     25         switch (Sn) {
     26         case 1:
     27             sign = '+';
     28             break;
     29         case 2:
     30             sign = '-';
     31             break;
     32         case 3:
     33             sign = '×';
     34             break;
     35         case 4:
     36             sign = '÷';
     37             break;
     38         }
     39         return sign;
     40     }
     41 
     42     public static  void main(String[] args) {
     43         // TODO Auto-generated method stub
     44         Scanner cin = new Scanner(System.in);
     45         //File file = new File("E:\szys.txt"); 
     46         int i = 0;
     47         int huida,score = 0;
     48          do
     49          {
     50              int  x = (int) (Math.random() * (100 - 1 )+ 1);  //产生1-100的随机数
     51              int  y = (int) (Math.random() * (100 - 1 )+ 1);  //产生1-100的随机数
     52              char sign = getCharRandom();
     53              /*
     54               * 判断乘法的范围*/
     55              switch(sign)
     56              {
     57              case '+':
     58                  question[i] = x +""+ sign +""+ y + "=";
     59                  huida = x + y;
     60                  answer[i] = huida;        
     61                  //System.out.println( "("+ (i+1) +")"+ x + " " + sign + " " + y + "="     );
     62                  i++;break;
     63              case '-':
     64                  if(x < y)                        //判断减数与被减数的大小关系
     65                  {
     66                      int temp;
     67                      temp = x;
     68                      x = y;
     69                      y = temp;
     70                  }
     71                  question[i] = x +""+ sign +""+ y + "=";
     72                  huida = x - y;
     73                  answer[i] = huida;    
     74                  //System.out.println( "("+ (i+1) +")"+ x + " " + sign + " " + y + "="     );
     75                  i++;break;
     76              case '×':
     77              {
     78                  x = (int) (Math.random() * (10 - 1 )+ 1);//新生成x,y<9的随机数
     79                  y = (int) (Math.random() * (10 - 1 )+ 1);
     80                  question[i] = x +""+ sign +""+ y + "=";
     81                  huida = x * y;
     82                  answer[i] = huida;    
     83                      //System.out.println( "("+ (i+1) +")"+ x + " " + sign + " " + y + "="     );
     84                  i++;
     85              };break;
     86              case '÷':
     87                  do                                             //循环生成除法
     88                  {
     89                      y = (int) (Math.random() * (10 - 1 )+ 1);
     90                      x = (int) (Math.random() * (9*y - 1 )+ 1);
     91                                             
     92                  }
     93                 while(x % y != 0) ;
     94                 question[i] = x +""+ sign+"" + y + "=";
     95                 huida = x / y;
     96                  answer[i] = huida;    
     97                 //System.out.println( "("+ (i+1) +")"+ x + " " + sign + " " + y + "="     );
     98                 i++;break;
     99                  }
    100         }
    101         while(i<10);
    102          
    103          
    104     try {
    105             BufferedWriter br= new BufferedWriter(new FileWriter("szys.txt")); 
    106             for(int k = 0;k<10;k++)
    107             {
    108                 br.write("("+(k+1)+")"+String.valueOf(question[k])+"*"+String.valueOf(answer[k]));//读取数组进缓冲区
    109                 br.newLine();//写入新的一行,换行
    110                 br.flush();//将缓冲区存入txt
    111             }    
    112         } catch (IOException e) {
    113         //文件读写异常
    114         e.printStackTrace();
    115     }
    116     
    117     try {
    118             String line = null;
    119             BufferedReader re = new BufferedReader(new FileReader("szys.txt"));//新定义
    120             while((line = re.readLine())!= null)
    121             {//逐行读取文件
    122                 String [] txt = line.split("\*");//以*为分界,将.txt分开成问题和答案两块
    123                 System.out.println(txt[0]);//输出题目
    124                 System.out.print("Your answer:");
    125                 String an = cin.next();
    126                 if(txt[1].equals(an))//判断答案与回答
    127                 {
    128                     System.out.println("回答正确!");
    129                     score++;
    130                 }
    131                 else
    132                     System.out.println("回答错误!正确答案:" + txt[1]);
    133             }
    134             System.out.println("共答对"+ score + "题,答错" + (10-score) + "题");
    135     }catch(IOException e)
    136         {
    137             e.printStackTrace();
    138         }
    139 
    140 }
    141 }

    【运行结果】

    【szys.txt】

  • 相关阅读:
    Seafile开源私有云自定义首页Logo图片
    启动seafile报错Error happened during creating seafile admin.
    jupyter的安装和使用(python3版)
    C#学习资料
    服务编排-以docker-swarm为例讲解
    docker swarm架构图
    VMware中安装centos7
    js获取数据:类似xpath
    博客园备份python程序
    解决docker中jenkins运行磁盘满的问题
  • 原文地址:https://www.cnblogs.com/flw0322/p/9964621.html
Copyright © 2011-2022 走看看