我尝试将所有pr.prinln(在代码底部)打印到屏幕上显示的result.txt,但是;当我运行代码时,生成result.txt但在正文中保持为空。请帮忙!
import java.io.*;
public class Assignment8
{
public static void main(String[] args)
{
try {
//create an InputStreamReader obj first
InputStreamReader ir = new InputStreamReader(System.in);
File outf=new File("result.txt");
PrintWriter pr = new PrintWriter(outf);
//create a BufferedReader obj
BufferedReader br = new BufferedReader(ir);
//variales
double b,a,d,w, interest;
int count =0;
double sumd =0.00;
double sumw =0.00;
double sumi =0.00;
double totalI;//format
System.out.print("\nEnter the starting balance on the account: $");
b = Double.parseDouble(br.readLine());
System.out.print("\nEnter the annual interest rate on the account (e.g. .04): ");
a = Double.parseDouble(br.readLine());
for(int i=1; i<4; i++)
{
count++;
//#3 read in the next data
System.out.print("\nMonth"+i);
System.out.print("\nTotal deposits for this month: $");
d = Double.parseDouble(br.readLine());
sumd += d;
System.out.print("Total withdrawals for this month: $");
w = Double.parseDouble(br.readLine());
sumw += w;
interest = b+(b+d-w)*a/12-b;
sumi +=interest;
}
totalI=+b+sumd-sumw+sumi;
System.out.println("\nQuarterly Savings Account Statement");
System.out.println("\nStarting balance: $ "+b);
System.out.println("Total deposits: + $ "+sumd);
System.out.println("Total withdrawals: - $ "+sumw);
System.out.println("Total interest: + $ "+sumi);
System.out.println(" ----------- ");
System.out.println("Ending balance: $ "+totalI);
pr.println("\nQuarterly Savings Account Statement");
pr.println("\nStarting balance: $ "+b);
pr.println("Total deposits: + $ "+sumd);
pr.println("Total withdrawals: - $ "+sumw);
pr.println("Total interest: + $ "+sumi);
pr.println(" ----------- ");
pr.println("Ending balance: $ "+totalI);
//double d = 3.14159;
//printf ("%.2f", d);
}
catch (IOException e)
{
System.out.println(e);
}
catch (ArithmeticException e)
{
System.out.println("Wrong, restart!");
}
}
}