Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 文件编写器在do while循环中无法正常工作_Java - Fatal编程技术网

Java 文件编写器在do while循环中无法正常工作

Java 文件编写器在do while循环中无法正常工作,java,Java,如果我在它编译的循环中设置构造函数,但它只写入最后的输入,那么我将无法将数据写入文件。如果将构造函数置于循环之外,则会出现问题,我无法找到解决方法。我一直在想方设法,但什么也没用 public static void main(String[] args) throws IOException { boolean isRegularClient; Scanner myScan = new Scanner (System.in); int userC

如果我在它编译的循环中设置构造函数,但它只写入最后的输入,那么我将无法将数据写入文件。如果将构造函数置于循环之外,则会出现问题,我无法找到解决方法。我一直在想方设法,但什么也没用

public static void main(String[] args) throws IOException {
        boolean isRegularClient;
        Scanner myScan = new Scanner (System.in);
        int userChoice;
        File myFile = new File ("invoices.txt");
        try {
            FileWriter myWriter = new FileWriter (myFile);}
        catch (IOException ex) {
            System.out.println("An error occurred.");}
        do{
            System.out.println ("Welcome to Marco's Candy Shop");
            System.out.println ("Our speciality and our unique product is the famous Mysterious Beans.");
            System.out.println ("How many lbs do you want? (only exact amounts)");
            while (!myScan.hasNextInt()){
                myScan.next();
            }
            int quantityCandy = myScan.nextInt();
            System.out.println ("We are giving 20% off to our regular customers. Are you a regular client?");
            String string1 = myScan.next();
            if ("Yes".equals(string1) || "yes".equals(string1)){
                isRegularClient = true;
            }
            else {
                isRegularClient = false;
            }
            System.out.println ("Please Enter your billing adress");
            String adress = myScan.next();
            System.out.println ("How much is your local tax rate? (enter as percent)");
            while (!myScan.hasNextDouble()){
                myScan.next();
            }
            double taxRate = myScan.nextDouble();
            Client newClient = new Client(adress,isRegularClient,quantityCandy,taxRate);
            final double pricexLB = 0.80;
            final double regularClientDiscount = 0.20;
            double orderPrice = (pricexLB * quantityCandy);
            double orderTax = (orderPrice*taxRate)/100;
            double finalCharge;
            if (isRegularClient){
                finalCharge = (orderPrice-(orderPrice*regularClientDiscount))+orderTax;
            }
            else {
                finalCharge = orderPrice+orderTax;
            }
            System.out.println ("You ordered " + quantityCandy + " lbs of the Mysterious Beans. You have to pay " + finalCharge + "$. This invoice will be sent to " + adress);
            try{
                myWriter.write ("You ordered " + quantityCandy + " lbs of the Mysterious Beans. You have to pay " + finalCharge + "$. This invoice will be sent to " + adress);
                myWriter.close();}
            catch (IOException e) {
                System.out.println("An error occurred.");}
            System.out.println ("If you want to make another order please enter 1, otherwise enter any number to exit");
            while (!myScan.hasNextInt()){
            myScan.next();}
            userChoice =  myScan.nextInt();}
        while (1 == userChoice);}
        
        
        
        
    }

您正在以写入模式打开fileWriter,它通常会清空文件并添加新内容。为此,您必须以追加模式打开文件

FileWriter将接受两个参数,第二个参数作为追加模式的布尔值,如果您在内部创建FileWriter,请尝试阻止它仅在该范围内可访问,您无法访问外部。因此,您也必须在try/catch中移动do/while块

try {
         FileWriter myWriter = new FileWriter (myFile, true);
         do{
            System.out.println ("Welcome to Marco's Candy Shop");
            System.out.println ("Our speciality and our unique product is the famous Mysterious Beans.");
            System.out.println ("How many lbs do you want? (only exact amounts)");
            while (!myScan.hasNextInt()){
                myScan.next();
            }
            int quantityCandy = myScan.nextInt();
            System.out.println ("We are giving 20% off to our regular customers. Are you a regular client?");
            String string1 = myScan.next();
            if ("Yes".equals(string1) || "yes".equals(string1)){
                isRegularClient = true;
            }
            else {
                isRegularClient = false;
            }
            System.out.println ("Please Enter your billing adress");
            String adress = myScan.next();
            System.out.println ("How much is your local tax rate? (enter as percent)");
            while (!myScan.hasNextDouble()){
                myScan.next();
            }
            double taxRate = myScan.nextDouble();
            Client newClient = new Client(adress,isRegularClient,quantityCandy,taxRate);
            final double pricexLB = 0.80;
            final double regularClientDiscount = 0.20;
            double orderPrice = (pricexLB * quantityCandy);
            double orderTax = (orderPrice*taxRate)/100;
            double finalCharge;
            if (isRegularClient){
                finalCharge = (orderPrice-(orderPrice*regularClientDiscount))+orderTax;
            }
            else {
                finalCharge = orderPrice+orderTax;
            }
            System.out.println ("You ordered " + quantityCandy + " lbs of the Mysterious Beans. You have to pay " + finalCharge + "$. This invoice will be sent to " + adress);
            try{
                myWriter.write ("You ordered " + quantityCandy + " lbs of the Mysterious Beans. You have to pay " + finalCharge + "$. This invoice will be sent to " + adress);
                myWriter.close();}
            catch (IOException e) {
                System.out.println("An error occurred.");}
            System.out.println ("If you want to make another order please enter 1, otherwise enter any number to exit");
            while (!myScan.hasNextInt()){
            myScan.next();}
            userChoice =  myScan.nextInt();}
        while (1 == userChoice);}
} catch (IOException ex) {
            System.out.println("An error occurred.");
}

有关更多信息,请参阅此部分。

do/while循环应位于try块内。这就是FileWriter的定义。但是“重要的”是您可以用两种方式之一打开输出流:“truncate”(默认)或“append模式”。您需要“附加模式”,例如
FileWriter fw=newfilewriter(fileName,true)。寻找细节。你是对的-但是你忘了提到OP也需要移动他的“do”循环;)谢谢你@paulsm4我忘了提那件事了