【问题标题】:How do I loop this I/O program successfully?如何成功循环这​​个 I/O 程序?
【发布时间】:2019-04-19 02:44:29
【问题描述】:

如何循环此代码,以便您输入的所有内容成功显示在文件“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);
}
}

【问题讨论】:

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


    【解决方案1】:

    你的方向是对的。您只需要对代码进行一些小的更改。具体来说,您正在为 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();
    }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多