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保存,无法进行小数结果的计算。

  • 相关阅读:
    Spring JDBC配置数据源
    Eclipse创建一个Maven Web项目
    部署基于Maven的war文件到Tomcat
    使用“mvn site-deploy”部署站点(WebDAV例子)
    生成基于Maven的项目文档站点
    将项目安装到Maven本地资源库
    使用Maven运行单元测试
    使用Maven清理项目
    使用Maven构建项目
    Dubbo的使用入门
  • 原文地址:https://www.cnblogs.com/lixv2018/p/9966578.html
Copyright © 2011-2022 走看看