四则运算随机生成100题

package demo;
   
   import java.io.BufferedReader;
   import java.io.BufferedWriter;
   import java.io.FileReader;
   import java.io.FileWriter;
   import java.io.IOException;
   import java.util.Scanner;
  
  public class sizeyunsuan {
  
      static String[] question = new String[100];
      static int[] answer = new int[100];
  
      public static int getRandom(int n, int m) {
          return (int) (Math.random() * (m - n) + n);
      }
  
      public static char getCharRandom() {
          char sign = 0;
          int Sn;
          Sn = getRandom(1, 5);
          switch (Sn) {
          case 1:
              sign = '+';
              break;
          case 2:
              sign = '-';
              break;
          case 3:
              sign = '×';
              break;
          case 4:
              sign = '÷';
              break;
          }
          return sign;
     }
  
      public static  void main(String[] args) {
          // TODO Auto-generated method stub
          @SuppressWarnings("resource")
        Scanner cin = new Scanner(System.in);
          //File file = new File("E:\\szys.txt"); 
          int i = 0;
          int huida,score = 0;
           do
           {
              int  x = (int) (Math.random() * (100 - 1 )+ 1);  //产生1-100的随机数
               int  y = (int) (Math.random() * (100 - 1 )+ 1);  //产生1-100的随机数
               char sign = getCharRandom();
               switch(sign)
               {
               case '+':
                   question[i] = x +""+ sign +""+ y + "=";
                   huida = x + y;
                  answer[i] = huida;        
                   //System.out.println( "("+ (i+1) +")"+ x + " " + sign + " " + y + "="     );
                   i++;break;
               case '-':
                   if(x < y)                        //判断减数与被减数的大小关系
                  {
                       int temp;
                       temp = x;
                      x = y;
                      y = temp;
                  }
                 question[i] = x +""+ sign +""+ y + "=";
                 huida = x - y;
                  answer[i] = huida;    
                  //System.out.println( "("+ (i+1) +")"+ x + " " + sign + " " + y + "="     );
                  i++;break;
             case '×':
              {
                 x = (int) (Math.random() * (10 - 1 )+ 1);//新生成x,y<9的随机数
                  y = (int) (Math.random() * (10 - 1 )+ 1);
                 question[i] = x +""+ sign +""+ y + "=";
                   huida = x * y;
                  answer[i] = huida;    
                      //System.out.println( "("+ (i+1) +")"+ x + " " + sign + " " + y + "="     );
                  i++;
              };break;
              case '÷':
                 do                                             //循环生成除法
                  {
                      y = (int) (Math.random() * (10 - 1 )+ 1);
                     x = (int) (Math.random() * (9*y - 1 )+ 1);
                                            
                   }
                 while(x % y != 0) ;
                  question[i] = x +""+ sign+"" + y + "=";
                 huida = x / y;
                   answer[i] = huida;    
                 //System.out.println( "("+ (i+1) +")"+ x + " " + sign + " " + y + "="     );
              i++;break;
                 }
         }
         while(i<10);
        
         
     try {
             @SuppressWarnings("resource")
            BufferedWriter br= new BufferedWriter(new FileWriter("sizeyunsuan.txt")); 
             for(int k = 0;k<10;k++)
             {
                 br.write("("+(k+1)+")"+String.valueOf(question[k])+"*"+String.valueOf(answer[k]));//读取数组进缓冲区
                 br.newLine();//写入新的一行,换行
                 br.flush();//将缓冲区存入txt
             }    
         } catch (IOException e) {
         //文件读写异常
         e.printStackTrace();
     }
     
     try {
             String line = null;
             @SuppressWarnings("resource")
            BufferedReader re = new BufferedReader(new FileReader("sizeyunsuan.txt"));//新定义
             while((line = re.readLine())!= null)
             {//逐行读取文件
                String [] txt = line.split("\\*");//以*为分界,将.txt分开成问题和答案两块
                 System.out.println(txt[0]);//输出题目
                 System.out.print("Your answer:");
                String an = cin.next();
                 if(txt[1].equals(an))//判断答案与回答
                 {
                     System.out.println("回答正确!");
                     score++;
                 }
                 else
                    System.out.println("回答错误!正确答案:" + txt[1]);
             }
            System.out.println("共答对"+ score + "题,答错" + (10-score) + "题");
    }catch(IOException e)
         {
             e.printStackTrace();
         }
 }
 }

我在这次试验卡了很长时间,由于文件的应用没有掌握太牢固以及一些知识点的应用有偏差;

使用BufferedWriter写入文本时不用将文本转换成字节数组,直接整行整行的写入,大大提供了写入效率。

猜你喜欢

转载自www.cnblogs.com/chenyuchun/p/9965570.html
今日推荐