gpt4 book ai didi

java - 如何实例化所有方法都可以使用的 PrintStream,并将 append 到同一文件?

转载 作者:行者123 更新时间:2023-11-30 06:03:54 25 4
gpt4 key购买 nike

我已经搜索了一段时间,但找不到我理解的方法。我得到的答案涉及几行代码和不同的打印机对象(FileOutputStream、FileWriter、bufferedwriter 等) PrintStream ps 用于程序中的其他两个方法。当我在每个方法中实例化时它工作正常,但随后我无法 append 到文件,它只是覆盖它。有没有一种简单的方法可以使用类变量来做到这一点?

import java.util.Scanner;
import java.io.PrintStream;

public class Main {

public static void main(String[] args) throws Exception {

Scanner sc = new Scanner(System.in);
PrintStream ps = new PrintStream("brooklynCarpet.txt");


double length, width, price, discount, area, carpetPrice, labor,
laborCost, installPrice, discountAmt, subTotal, tax, grandTotal;

System.out.println("Length of room (feet)?");
length = sc.nextDouble();

System.out.println("Width of room (feet)?");
width = sc.nextDouble();

System.out.println("Customer discount (percent)?");
discount = sc.nextDouble();

System.out.println("Cost per square foot (xxx.xx)?");
price = sc.nextDouble();

area = length * width;
carpetPrice = calcPrice(area, price);
laborCost = calcLabor(area);
installPrice = calcInstall(laborCost, carpetPrice);
discountAmt = calcDisc(installPrice, discount);
subTotal = calcSub(installPrice, discountAmt);
tax = calcTax(subTotal);
grandTotal = subTotal + tax;
labor = 0.35;

printHeader(length, width, area);

printBill(price, carpetPrice, labor, laborCost, installPrice,
discount, discountAmt, subTotal, tax, grandTotal);

}


// first function calculates carpet price.

public static double calcPrice(double area, double price) {

double carpetPrice;

carpetPrice = area * price;

return carpetPrice;

}

// second function calculates labor cost

public static double calcLabor(double area) {

double labor = 0.35;

double laborCost = area * labor;

return laborCost;
}

// third function calculates installed price ie. carpet + labor

public static double calcInstall(double carpetPrice, double laborCost) {

double installedPrice = carpetPrice + laborCost;

return installedPrice;
}

// fourth function calculates discount ie % 0.01 * installed

public static double calcDisc(double installedPrice, double discount) {

double discountAmt = installedPrice * (discount * 0.01);

return discountAmt;
}

// fifth function calculates subtotal ie installed - discountamt

public static double calcSub(double installedPrice, double discountAmt) {

double subTotal = installedPrice - discountAmt;

return subTotal;
}

// sixth function calculates tax. subTotal * 0.085

public static double calcTax(double subTotal) {

double taxRate = 0.085;

double tax = subTotal * taxRate;

return tax;
}

//想要用下一个函数打印

public static void printHeader(double length, double width, double area) throws Exception{

ps.printf("CARPET STORE\n");
ps.printf("\n\n\t\t\tMEASUREMENT");
ps.printf("\n\nLength\t\t\t\t %.2f feet", length);
ps.printf("\n\nWidth\t\t\t\t %.2f feet", width);
ps.printf("\n\nArea\t\t\t\t %.2f square feet", area);

}

//还有这个

public static void printBill(double price, double carpetPrice, double labor
, double laborCost, double installPrice, double discount,
double discountAmt, double subTotal, double tax, double grandTotal) throws Exception{

ps.printf("\t\t\t\t\t CHARGES\n\nDESCRIPTION\t\t\tCOST/SQ.FT.\t\t\tCHARGE/ROOM");
ps.printf("\nCARPET\t\t\t\t\t" + price + "\t\t\t\t\t" + carpetPrice);
ps.printf("\nLabor\t\t\t\t\t" + "%.2f" + "\t\t\t $" + "%.2f", labor, laborCost);
ps.printf("\n\t\t\t\t\t\t\t\t\t\t----------");
ps.printf("\nINSTALLED PRICE\t\t " + "$%.2f", installPrice);
ps.printf("\nDiscount\t\t\t\t" + "%.2f" + " %%" + "\t\t\t\t%.2f" ,discount, discountAmt);
ps.printf("\nSUBTOTAL\t\t\t\t\t\t\t\t\t" + "%.2f\n", subTotal);
ps.printf("\nTax\t\t\t\t\t\t\t\t\t\t " + "%.2f\n", tax);
ps.printf("\nTotal\t\t\t\t\t\t\t\t\t $" + "%.2f\n", grandTotal);

}

}

最佳答案

我认为你有三个选择,

选项 1:将 PrintStream 包裹在 FileOutputStream 周围处于追加模式。

选项 2:创建一个 PrintStream 全局变量。

选项 3:使用 System.setOut(PrintStream) - 然后你可以在任何地方调用 System.out.printf 来写入你的 PrintStream (老实说,这只是执行选项 2 的另一种方法)。

关于java - 如何实例化所有方法都可以使用的 PrintStream,并将 append 到同一文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51737671/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com