zoukankan      html  css  js  c++  java
  • 四则运算文件流操作

    使用System.setOut()方法改变输出流

    比如 PrintStream outputStream=new PrintStream(new File(“ab.txt”));System.setOut(outputStream);//这时,系统的标志输出流就定向到了ab.txt 文件中。

    如果想继续向控制台输出信息,则需要在setOut方法调用之前保存标准的输出流,

    即:PrintStream out = System.out; System.setOut(out);

    生成三道题实例:

    package yunsuan;
    import java.io.BufferedReader;
    import java.io.PrintStream;
    import java.util.Scanner;
    import java.io.FileReader;
    import java.io.IOException;
    public class Yunsuan {
     public static void main(String[] args) throws IOException {
      int a1,a2;
      int b;
      int num=0;
      int i=0;
            String f[]=new String[4];
            f[0]="+";
            f[1]="-";
            f[2]="*";
            f[3]="/";
            int a[]=new int[100];
            //System.out.println("请选择100以内或1000以内");
            //m=sc.nextInt()+1;
            //System.out.println("请输入要产生的题数");
            //n=sc.nextInt();
            PrintStream out = System.out;
            PrintStream ps = new PrintStream("Test.txt");
            System.setOut(ps);
            for(i=0;i<3;i++)
            {
            a1=(int)(Math.random()*100);
            a2=(int)(Math.random()*100);
            if(a1<a2) {
             int n;
             n=a1;a1=a2;a2=n;
            }
            b=(int)(Math.random()*4);
            System.out.println("第"+(i+1)+"题:"+a1+f[b]+a2+"=");
            System.out.println("*");
            switch(b) {
            case 0:a[i]=(a1+a2);break;
            case 1:a[i]=(a1-a2);break;
            case 2:a[i]=(a1*a2);break;
            case 3:a[i]=(a1/a2);break;
            }
            }
            ps.close();
           
            System.setOut(out);
            System.out.println("题目:");
            BufferedReader br = new BufferedReader(new FileReader("Test.txt"));
            String line = "";
            Scanner sc=new Scanner(System.in);
            int answer[]=new int[100];
            int m=0;
            while((line = br.readLine()) != null){
              if(line.charAt(0)=='*') {
               System.out.print("请输入答案:");
               //System.out.println(a[m]);
                  int n;
                  n=sc.nextInt();
                  answer[m]=n;
                  if(answer[m]==a[m]) {num++;System.out.println("计算正确!");}
                  else
                   System.out.println("计算错误!");
                  m++;
              }
              else {
               System.out.println(line);
              }
               
              
      }
            sc.close();
            br.close();
            System.out.println("共做了"+m+"题");
            System.out.println("共答对"+num+"题");
     }
    }
     
     

    存在问题:结果使用int保存,无法进行小数结果的计算。

  • 相关阅读:
    cocos2dx-lua捕获用户touch事件的几种方式
    Java并发编程之闭锁CountDownLatch简单介绍
    opencv视频播放
    完全备份、差异备份以及增量备份的区别
    如何实现文件增量同步——算法
    Oracle提示“资源正忙,需指定nowait”的解决方案
    oracle之报错:ORA-00054: 资源正忙,要求指定 NOWAIT
    一次oracle大量数据删除经历
    rownum的使用-分页
    sql语句分页多种方式ROW_NUMBER()OVER
  • 原文地址:https://www.cnblogs.com/lixv2018/p/9966578.html
Copyright © 2011-2022 走看看