gpt4 book ai didi

java - 如果我希望能够在文件 "total sales"中显示 "Cart.txt",我该怎么做?

转载 作者:太空宇宙 更新时间:2023-11-04 10:02:30 25 4
gpt4 key购买 nike

我希望 Cart.txt 文件能够写入文件本身中的总销售额。现在的文件只是说:

3,2,Shoes
3,4,Shirt
2,5,Car

这是当前的输出:

run:
Enter how many items you are buying
3
Enter the items you are buying, structured as followed
Quantity,Price,Item Name:
3,2,Shoes
3,4,Shirt
2,5,Car
Those values were written to Cart.txt
Sold 3 of Shoes at $2.00 each.
Sold 3 of Shirt at $4.00 each.
Sold 2 of Car at $5.00 each.
Total sales: $28.00

这是代码本身:

package shop;

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 Shop
{

public static void main(String[] args)
{
String fileName = "Cart.txt";
PrintWriter outputStream = null;

try
{
outputStream= new PrintWriter (fileName);
}
catch (FileNotFoundException e)
{
System.out.println("Error opening the file "+ fileName);
System.exit(0);
}

System.out.println("Enter how many items you are buying");
Scanner keyboard = new Scanner (System.in);
Scanner intinput = new Scanner (System.in);
int input = intinput.nextInt();

System.out.println("Enter the items you are buying, structured as followed"
+ " \nQuantity,Price,Item Name:");


for(int count=1; count<=input; count++)
{
String line = keyboard.nextLine();
outputStream.println(line);

}

outputStream.close();
System.out.println("Those values were written to "+ fileName);

try
{
Scanner inputStream = new Scanner(new File(fileName));
String line = inputStream.toString();
double total = 0;
for(int count=1; count<=input; count++)
{

line = inputStream.nextLine();
String[] ary = line.split (",");
int quantity = Integer.parseInt (ary[0]);
double price = Double.parseDouble(ary[1]);
String description = ary[2];
System.out.printf("Sold %d of %s at $%1.2f each. \n",
quantity, description, price);
total += quantity * price;

}

System.out.printf("Total sales: $%1.2f\n", total);
inputStream.close();

}
catch (FileNotFoundException e)
{
System.out.println("Cannot find file " + fileName);
}
catch (IOException e)
{
System.out.println("Problem with input file " + fileName);
}

}
}

最佳答案

执行以下操作:

  1. 计算总数后写入文件:outputStream.println(总计);
  2. 将总计写入文件后关闭outputStream
<小时/>

编辑:

在此代码块中进行以下更改:

    System.out.println("Enter the items you are buying, structured as 
followed" + " \nQuantity,Price,Item Name:");
double tot = 0.0;
for (int count = 1; count <= input; count++) {
String line = keyboard.nextLine();
outputStream.println(line);
String[] arr = line.split(",");
tot += (Integer.parseInt(arr[0]) * Double.parseDouble(arr[1]));
}
outputStream.println("Total sales: $" + tot);
outputStream.close();
System.out.println("Those values were written to " + fileName);

这里我们计算所有条目的总数,然后将其写入文件一次。

输出

3,2,Shoes

3,4,Shirt

2,5,Car

Total sales: $28.0

关于java - 如果我希望能够在文件 "total sales"中显示 "Cart.txt",我该怎么做?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53325473/

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