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("\r\n"); 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("\r\n"); 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("\r\n"); 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("\r\n"); 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