import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
import java.io.*;
public class suijishengcheng {
//四则运算计算
int a,b;
public int add(int a,int b) {
return a+b;
}
public int reduce(int a,int b)
{
return a-b;
}
public int multiplication(int a,int b)
{
return a*b;
}
public int division(int a,int b)
{
if(b!=0)
return a/b;
else return 0;
}
public static void main(String[] args) {
int sum = 0;//记录总成绩,每题10分
int[] jieguo = new int[1000];
suijishengcheng sj = new suijishengcheng();//定义对象,便于四则运算加减乘除的调用
Scanner in = new Scanner(System.in);
PrintWriter out = null;//输出文件的使用
try {
out = new PrintWriter("Test.txt");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (int i = 1;i <=100; i++) {
int a = (int) Math.round(Math.random() * 100);
int b = (int) Math.round(Math.random() * 100);
int m= (int) Math.round(Math.random() * 3);
switch(m)
{
case 0:
{while(b==0){ b = (int) Math.round(Math.random() * 100); }
jieguo[i] = sj.division(a, b);
out.println(a+"/"+b+"=");
break;
}
case 1:
{
jieguo[i] =sj.multiplication(a, b);
out.println(a+"*"+b+"=");
break;
}
case 2:
jieguo[i] = sj.add(a, b);
out.println(a+"+"+b+"=");
break ;
case 3:
jieguo[i] = sj.reduce(a, b);
out.println(a+"-"+b+"=");
break ;
}
}
//System.out.println("成绩"+sum);
//out.println("成绩:"+sum);
out.close();
try
{
FileReader fr = new FileReader("Test.txt");//需要读取的文件路径
BufferedReader br = new BufferedReader(fr);
String s = br.readLine();
for(int j = 1;j<=100;j++) {
while(s!=null)//如果当前行不为空
{
System.out.println(s);//打印当前行
/*int c = in.nextInt();
if(c == jieguo[j]) {
sum += 10;
System.out.println("恭喜答案正确");
}
else {
System.out.println("抱歉,答案错误");
}*/
s= br.readLine();//读取下一行
}
}
br.close();//关闭BufferReader流
fr.close(); //关闭文件流
}catch(IOException e)//捕捉异常
{
System.out.println("指定文件不存在");//处理异常
}
}
课上没有做出的原因主要是对文件流的使用很生疏不熟练
原文:https://www.cnblogs.com/shnm/p/9966828.html