zoukankan      html  css  js  c++  java
  • 100道题的代码

    完成时间:15:40

    package Class_fifth;
    
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.Random;
    import java.util.Scanner;
    
    public class The_Class_Test {
    
        //随机生成100道题目
        public void scanf() throws IOException {
            String str[]=new String[100];
            String answer[]= new String[100];
            int n = 0;
            int x,y,m,mm;
            Random random = new Random(System.currentTimeMillis());
            String ch[] = new String[] {"-","+","/","*"};
            for(int i = 1;i<=100;i++) {
            
                     x = random.nextInt(100)+1;
                     y = random.nextInt(100)+1;
                     m = random.nextInt(20);
                     mm = random.nextInt(4);
                     if(ch[mm]=="*"){
                        str[n]=  x+"*"+y+"=";
                        answer[n]=x*y+"";}
                      if(ch[mm]=="/"){
                        str[n]=  x*m+ch[mm]+x+"=";
                        answer[n]=m+"";
                      }
                      if(ch[mm]=="+"){
                         str[n] = x+"+"+y+"=";
                         answer[n]=x+y+"";
                      }
                      if(ch[mm]=="-")
                      {
                          if(x>y)
                             str[n]= x+"-"+y+"=";
                          else 
                              str[n]= y+"-"+x+"=";
                          answer[n]=Math.abs(x-y)+"";
                      }
                      n++;
            }
            //将题目输入test.txt文本中
             FileWriter fw = null;
                try {
                    //创建字符输出流
                    fw = new FileWriter("test.txt");
                    for(int i = 0;i<100;i++)
                    fw.write(str[i]+"\r\n");
                } catch (IOException ioe) {
                    ioe.printStackTrace();                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
                } finally {
                    //使用finally块来关闭文件输出流
                    if (fw != null) {
                        fw.close();
                    }
                }
              //将答案添加到answer.text文本中
                FileWriter fwas = null;
                try {
                    //创建字符输出流
                    fwas = new FileWriter("answer.txt");
                    for(int i = 0;i<100;i++)
                    fwas.write(answer[i]+"\r\n");
                } catch (IOException ioe) {
                    ioe.printStackTrace();
                } finally {
                    //使用finally块来关闭文件输出流
                    if (fwas != null) {
                        fwas.close();
                    }
                }
            }
        
        public void print() throws IOException {
            //输出题目进入控制台
            String str[]= new String[100];
            int n =0;
            File a=new File("test.txt");
               FileInputStream b = new FileInputStream(a);
               InputStreamReader c=new InputStreamReader(b,"UTF-8");
               {
                BufferedReader bufr =new BufferedReader(c);
                String line = null;
                while((line = bufr.readLine())!=null){
                str[n]=line;
                n++;
                }
                bufr.close();            
               }
               c.close();
               b.close();
               //把答案放进字符串数组中
               String answer[]= new String[100];
                 n =0;
                File as=new File("answer.txt");
                   FileInputStream bs = new FileInputStream(as);
                   InputStreamReader cs=new InputStreamReader(bs,"UTF-8");
                   {
                    BufferedReader bufrs =new BufferedReader(cs);
                    String lines = null;
                    while((lines = bufrs.readLine())!=null){
                        answer[n]=lines;
                    n++;
                    }
                    bufrs.close();            
                   }
                   cs.close();
                   bs.close();
            
                   
             //输出题目,并设置输出的格式
                   Scanner cin = new Scanner(System.in);
                   int score =0;
                   for(int i=0;i<100;i++) {
                       String Answer;
                       System.out.println(str[i]);
                       Answer = cin.nextLine();
                       if(Answer.equals("*")) {
                           System.out.println("您一共做了"+i+"道题");
                           System.out.println("您做错"+(i-score)+"题");
                           System.out.println("您做对"+score+"题");
                           System.exit(0);
                       }
                       else {
                           if(Answer.equals(answer[i]))
                               score++;
                       }
                   }
        }
        public static void main(String[] args) throws IOException {
            The_Class_Test a = new The_Class_Test();
            a.scanf();
            a.print();
        }
    }
  • 相关阅读:
    TCP系列24—重传—14、F-RTO虚假重传探测
    TCP系列23—重传—13、RACK重传
    TCP系列22—重传—12、Forward Retransmit
    TCP系列21—重传—11、TLP
    TCP系列20—重传—10、早期重传(ER)
    TCP系列19—重传—9、thin stream下的重传
    TCP系列18—重传—8、FACK及SACK reneging下的重传
    TCP系列17—重传—7、SACK下的重传
    TCP系列16—重传—6、基础快速重传(Fast Retransmit)
    Centos 6.2 安装mysql5.5
  • 原文地址:https://www.cnblogs.com/zhangzhongkun/p/9966501.html
Copyright © 2011-2022 走看看