如何成功循环此I / O程序?

时间:2018-11-15 22:43:48

标签: java for-loop while-loop exception-handling io

如何循环这段代码,以便您输入的所有内容都成功显示在文件“ list.txt”中,以便您可以请求添加其他项以继续?

这是我的提示,如果有帮助: “写一个程序来记录在商店购买的商品。对于每次购买,请从键盘上读取商品的名称,价格和购买数量。计算购买成本(购买数量乘以价格),然后全部写上将此数据保存到文本文件中,然后在屏幕上显示此信息和当前总费用。输入完所有项目后,将总费用写入屏幕和文件中。由于我们要记住所有购买的商品,您应该将新数据附加到文件末尾。”

package market;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.File;
import java.io.PrintWriter;
import java.util.Scanner;


public class Market 
{
public static void main(String[] args)
{

    String fileName = "List.txt";
    PrintWriter outputStream = null;
    try
    {
        outputStream= new PrintWriter (fileName);
    }
    catch (FileNotFoundException e)
    {
        System.out.println("Error opening the file "+ fileName);
        System.exit(0);
    }
    catch (IOException e)
    {
       System.out.println("Problem with input from file "+fileName);
    }
    Scanner keyboard = new Scanner (System.in);

    double cost;
    double total=0;

    boolean done = false;
    while(!done)
    {
         String name = keyboard.next(); 
         int quantity = keyboard.nextInt();
         double price = keyboard.nextDouble();
         cost = quantity*price;
         total += cost;

         System.out.println(quantity + " " + name + " = " + cost);
         System.out.println("Subtotal = " + total);

         outputStream.println(quantity + " " + name + " = " + cost);
         outputStream.println("Cost = " + cost);
         outputStream.println("Subtotal = " + total);
         outputStream.close();


    }
    System.out.println("Would you like another item?");
    System.out.println("Total = " + total);
}
}

1 个答案:

答案 0 :(得分:0)

您的方向正确。您只需要对代码进行一些小的更改。具体来说,您正在检查while循环的布尔变量done。因此,基本上,您需要在每个循环中根据用户的输入来修改此变量值,

package market;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.File;
import java.io.PrintWriter;
import java.util.Scanner;


public class Market 
{
public static void main(String[] args)
{

    String fileName = "List.txt";
    PrintWriter outputStream = null;
    try
    {
        outputStream= new PrintWriter (fileName);
    }
    catch (FileNotFoundException e)
    {
        System.out.println("Error opening the file "+ fileName);
        System.exit(0);
    }
    catch (IOException e)
    {
       System.out.println("Problem with input from file "+fileName);
    }
    Scanner keyboard = new Scanner (System.in);

    double cost;
    double total=0;

    boolean done = false;
    while(!done)
    {
         String name = keyboard.next(); 
         int quantity = keyboard.nextInt();
         double price = keyboard.nextDouble();
         cost = quantity*price;
         total += cost;

         System.out.println(quantity + " " + name + " = " + cost);
         System.out.println("Subtotal = " + total);

         outputStream.println(quantity + " " + name + " = " + cost);
         outputStream.println("Cost = " + cost);
         outputStream.println("Subtotal = " + total);

         //outputStream.close();
         //don't close the stream till you are totally done. Close this outside the while loop

        //This block of code needs to be inside the loop and not outside 
        System.out.println("Would you like another item?");
        //Modify the done variable here
        done = keyboard.nextLine().equals("Y")?false:true; // if user enters Y, loop continues, else done will be true and the loop won't continue
        System.out.println("Total = " + total);
        ////////////////////////////////////////////////////////////////
    }
    outputStream.close();
}

}