• 四则运算缓冲流


    package sizeyuansuan;
    import java.io.*;
    import java.util.*;
    public class Sizeyuansuan {
        public static void main(String[] args)throws IOException
        {
            FileOutputStream outfile= new FileOutputStream(new File("sizeyuansuan.txt"));
            PrintStream printStream= new PrintStream(outfile);
            FileInputStream infile= new FileInputStream("sizeyuansuan.txt");
            InputStreamReader reader = new InputStreamReader(infile, "UTF-8");
             StringBuffer buffer = new StringBuffer();
            Scanner in=new Scanner(System.in);
            int a=0;
            int b=0;
            int sum;
            int count=0;
            double[] num=new double[100];
            int i;
            for(i=0;i<100;i++)
            {
                a= (int)(Math.random()*10+1);
                b=(int)(Math.random()*10+1);
                int number=(int)(Math.random()*30);
                if(number%4==0)
                {
                    sum=a+b;
                    printStream.print(a);
                    printStream.print("+");
                    printStream.print(b);
                    printStream.print("=");
                    printStream.print(sum);
                    printStream.append("
    ");
                    num[i]=sum;
                }
                if(number%4==1)
                {
                    sum=a-b;
                    printStream.print(a);
                    printStream.print("-");
                    printStream.print(b);
                    printStream.print("=");
                    printStream.print(sum);
                    printStream.append("
    ");
                    num[i]=sum;
                }
                if(number%4==2)
                {
                    sum=a*b;
                    printStream.print(a);
                    printStream.print("*");
                    printStream.print(b);
                    printStream.print("=");
                    printStream.print(sum);
                    printStream.append("
    ");
                  num[i]=sum;
                }
                if(number%4==3)
                {
                    sum=a/b;
                    printStream.print(a);
                    printStream.print("/");
                    printStream.print(b);
                    printStream.print("=");
                    printStream.print(sum);
                    printStream.append("
    ");
                    num[i]=sum;
                }
            }
            printStream.close();
            int test;
            double Sum;
            for(i=0;i<100;i++)
            {
            while ((test= reader.read()) != '=')
            {
                System.out.print((char)test);
            }
            Sum=in.nextDouble();
            if(Sum==num[i])
            {
                System.out.println("ture");
                count++;
            }
            else
            {
                System.out.println("false");
            }
            }
            System.out.println("ture:"+count+"   false:"+(100-count));
            reader.close();
        }
    }

    遇到的困难:1.报错thread "main" java.lang.ArithmeticException: 意思为:运算异常,后我改正了随机数的生成,修改了错误。

                          2.不会缓冲流文本输入,后在网络上学习,学会了缓冲流的输入。

                          3.长数组赋初值初始化麻烦,后采用这种方法初始化double[] num=new double[100];

                  备注:除法答案对比无法完成,因为除法答案的小数位除不尽时无法判断。

    完成时间:  20:00    

  • 相关阅读:
    关于git 拉取的时候一直弹输入密码的问题
    开始日期结束日期check问题
    关于boostrap 排版问题
    【DP_树形DP专题】题单总结
    【DP_背包专题】 背包九讲
    Ubuntu不卸载ibus前提下安装搜狗输入法
    Ubuntu下Java环境配置
    Ubuntu下gcc及g++环境配置
    Ubuntu下VIM(GVIM)环境配置
    PAT 1065 A+B and C (64bit) (20)
  • 原文地址:https://www.cnblogs.com/my---world/p/9965846.html
Copyright © 2020-2023  润新知