流与文件的四则运算
package 文件操作流;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Random;
import java.util.Scanner;
import java.util.Scanner;
public class SZYSfile {
public static void main(String args[])throws IOException,ClassNotFoundException {
File f = new File("D:\\","Test.txt");
f.createNewFile();
FileOutputStream fis = new FileOutputStream(f);
OutputStreamWriter osw = new OutputStreamWriter(fis);
int[] answer = new int [100];//存答案
for(int i = 0 ; i < 100;i++) {
int a = new Random().nextInt(10);//随机数a
int b = new Random().nextInt(10);//随机数b
int suijifu = new Random().nextInt(4);//随机运算符
String symbol=" ";
switch(suijifu) {
case(0):
{symbol = "+";answer[i]=a+b; break;}
case(1):
{symbol = "-";answer[i]=a-b; break;}
case(2):
{symbol ="×";answer[i]=a*b; break;}
case(3):
if(b==0) { symbol = "+";answer[i]=a+b; break;}
else
if(a%b!=0) {symbol = "-";answer[i]=a-b; break;}
else
if(a%b==0) {symbol = "÷";answer[i]=a/b; break;}
}
osw.write(a+symbol+b+"=");
osw.write("\r\n");
}
osw.close();
//读取文件中的运算
File file = new File("D:\\Test.txt");
int i=0;
int score = 0;
if(file.exists()){
try {
FileReader fileReader = new FileReader(file);
BufferedReader br = new BufferedReader(fileReader);
String lineContent = null;
while((lineContent = br.readLine())!=null){
System.out.println(lineContent);
Scanner input = new Scanner(System.in);
int a=input.nextInt();
if(a==answer[i]) {
System.out.println("回答正确!");
score++;
}
else
{System.out.println("回答错误! 答案是"+answer[i]);}
i++;
}
br.close();
fileReader.close();
} catch (FileNotFoundException e1) {
System.out.println("no this file");//文件不存在抛异常
e1.printStackTrace();
} catch (IOException e1) {
System.out.println("io exception");
e1.printStackTrace();
}
} System.out.println("分数为:"+score);
}
}
public static void main(String args[])throws IOException,ClassNotFoundException {
File f = new File("D:\\","Test.txt");
f.createNewFile();
FileOutputStream fis = new FileOutputStream(f);
OutputStreamWriter osw = new OutputStreamWriter(fis);
int[] answer = new int [100];//存答案
for(int i = 0 ; i < 100;i++) {
int a = new Random().nextInt(10);//随机数a
int b = new Random().nextInt(10);//随机数b
int suijifu = new Random().nextInt(4);//随机运算符
String symbol=" ";
switch(suijifu) {
case(0):
{symbol = "+";answer[i]=a+b; break;}
case(1):
{symbol = "-";answer[i]=a-b; break;}
case(2):
{symbol ="×";answer[i]=a*b; break;}
case(3):
if(b==0) { symbol = "+";answer[i]=a+b; break;}
else
if(a%b!=0) {symbol = "-";answer[i]=a-b; break;}
else
if(a%b==0) {symbol = "÷";answer[i]=a/b; break;}
}
osw.write(a+symbol+b+"=");
osw.write("\r\n");
}
osw.close();
//读取文件中的运算
File file = new File("D:\\Test.txt");
int i=0;
int score = 0;
if(file.exists()){
try {
FileReader fileReader = new FileReader(file);
BufferedReader br = new BufferedReader(fileReader);
String lineContent = null;
while((lineContent = br.readLine())!=null){
System.out.println(lineContent);
Scanner input = new Scanner(System.in);
int a=input.nextInt();
if(a==answer[i]) {
System.out.println("回答正确!");
score++;
}
else
{System.out.println("回答错误! 答案是"+answer[i]);}
i++;
}
br.close();
fileReader.close();
} catch (FileNotFoundException e1) {
System.out.println("no this file");//文件不存在抛异常
e1.printStackTrace();
} catch (IOException e1) {
System.out.println("io exception");
e1.printStackTrace();
}
} System.out.println("分数为:"+score);
}
}
![](https://img2018.cnblogs.com/blog/1279396/201811/1279396-20181115221703455-2129712194.png)
![](https://img2018.cnblogs.com/blog/1279396/201811/1279396-20181115221802843-1268349344.png)