import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
/*项目根路径下有个questions.txt文件内容如下:
5+5
150-25
155*155
2555/5
要求:读取内容计算出结果,将结果写入到results.txt文件中*/
public class Duxieyunsuan {
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new FileReader("questions.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("results.txt"));
String s;
int n =0;
while((s=bf.readLine())!=null){
n++;
String[] s1 = s.split("\\+|\\-|\\*|\\/");
if(n==1){
int a = Integer.parseInt(s1[0]) + Integer.parseInt(s1[1]);
bw.write("5+5="+a);
bw.newLine();
}
if(n==2){
int b = Integer.parseInt(s1[0]) - Integer.parseInt(s1[1]);
bw.write("150-25="+b);
bw.newLine();
}
if(n==3){
int c = Integer.parseInt(s1[0]) * Integer.parseInt(s1[1]);
bw.write("155*155="+c);
bw.newLine();
}
if(n==4){
int d = Integer.parseInt(s1[0]) / Integer.parseInt(s1[1]);
bw.write("2555/5="+d);
bw.flush();
}
}
bw.close();
bf.close();
}
} |
|