zoukankan      html  css  js  c++  java
  • 流与文件的四则运算

    package 文件操作流;
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.OutputStreamWriter;
    import java.util.Random;
    import java.util.Scanner;
    public class SZYSfile {
     public static void main(String args[])throws IOException,ClassNotFoundException {
      
      File f = new File("D:\","Test.txt");
      f.createNewFile();
      FileOutputStream fis = new FileOutputStream(f);
      OutputStreamWriter osw = new OutputStreamWriter(fis);
      int[] answer = new int [100];//存答案
      for(int i = 0 ; i < 100;i++) {
       int a = new Random().nextInt(10);//随机数a
       int b = new Random().nextInt(10);//随机数b
       int suijifu = new Random().nextInt(4);//随机运算符
       String symbol=" ";
      switch(suijifu) {
          case(0):
               {symbol = "+";answer[i]=a+b;   break;}
          case(1):
               {symbol = "-";answer[i]=a-b;   break;}
          case(2):
               {symbol ="×";answer[i]=a*b;   break;}
          case(3):
                     if(b==0) { symbol = "+";answer[i]=a+b;   break;}
                      else
                       if(a%b!=0) {symbol = "-";answer[i]=a-b;   break;}
                   else
                    if(a%b==0) {symbol = "÷";answer[i]=a/b;   break;}
      } 
      osw.write(a+symbol+b+"=");
      osw.write(" ");
      }
      osw.close();
      //读取文件中的运算
      File file = new File("D:\Test.txt");
            int i=0;
           
            int score = 0;
            if(file.exists()){ 
                try { 
                    FileReader fileReader = new FileReader(file); 
                    BufferedReader br = new BufferedReader(fileReader); 
                    String lineContent = null; 
                    while((lineContent = br.readLine())!=null){ 
                        System.out.println(lineContent);
                      
                        Scanner input = new Scanner(System.in);
                        int a=input.nextInt();
                       
                        if(a==answer[i]) {
                         System.out.println("回答正确!");
                         score++;
                        }
                        else
                         {System.out.println("回答错误! 答案是"+answer[i]);}
                        
                        i++;
                    } 
                    br.close(); 
                    fileReader.close(); 
                   
                } catch (FileNotFoundException e1) { 
                    System.out.println("no this file");//文件不存在抛异常 
                    e1.printStackTrace(); 
                } catch (IOException e1) { 
                    System.out.println("io exception"); 
                    e1.printStackTrace();
                   
                }
            } System.out.println("分数为:"+score);
       
        }
    }
  • 相关阅读:
    canvas粒子时钟
    数组复制
    对象拷贝
    不常见但很有用的chrome调试工具使用方法
    变形transform的副作用
    CSS页面渲染优化属性will-change
    CSS实现导航条Tab切换的三种方法
    CSS两端对齐
    CSS倒影
    CSS滤镜
  • 原文地址:https://www.cnblogs.com/zjl-0217/p/9966473.html
Copyright © 2011-2022 走看看