求完整的过程辛苦各位了

img


编程读取销售数据,输出指定型号商品的销售总额。例如:输出A001,期待输出为10500。

你可以参考一下,希望采纳
路径换成你的文件所在绝对路径即可

import java.io.*;
import java.util.*;

public class Demo{

    public static void main(String[] args) throws IOException {
        FileReader fr = new FileReader("D:/IDEA/idea_Demo/.idea/test.txt");
        BufferedReader br = new BufferedReader(fr);
        String s = br.readLine();// 读取文件的1行

        Map<String, Double> map = new HashMap<>();
        double sum = 0;

        while (s != null) {
            String[] info = s.split("\t");
            String name = info[0];
            double sales = Double.parseDouble(info[1]);
            if (map.containsKey(name)) {
                double oldValue = map.get(name);
                map.put(name, sales + oldValue);
            } else {
                map.put(name, sales);
            }
            s = br.readLine();
        }
        br.close();


        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.print("输入指定商品型号:");
            String id = sc.nextLine();

            if (map.containsKey(id)) {
                System.out.println("销售总额是:" + map.get(id));
            } else {
                System.out.println("找不到该型号");
            }
        }
    }
}

img

把test.txt放到D盘,文件内容如下:

A001    2500    1
A001    5000    2
A001    3000    3
A002    1200    2
A002    5000    3
A003    600    1
A003    800    3

完整实现代码如下:

    public static void main(String[] args) throws Exception {
        Map<String, Double> map = new HashMap<>();
        File file = new File("D:\\test.txt");
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
        String line = null;
        while((line = br.readLine())!=null) {
            String[] lineArr = line.split("\t");
            double newMoney = Double.parseDouble(lineArr[1]);
            map.compute(lineArr[0], (type, money)->money==null?newMoney:(money+newMoney));
        }

        System.out.print("请输入商品型号:");
        Scanner scanner = new Scanner(System.in);
        String type = scanner.nextLine();
        if(map.containsKey(type)) {
            System.out.println(map.get(type));
        }
    }

运行结果如下:

请输入商品型号:A001
10500.0

img

如有帮助,请采纳,十分感谢!

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

class Data {
    private String id;// 型号
    private double money;// 销售额
    private int month;// 月份

    public Data(String id, double money, int month) {
        super();
        this.id = id;
        this.money = money;
        this.month = month;
    }
    public String getId() {
        return id;
    }

    public double getMoney() {
        return money;
    }

}

public class Answer7723060 {

    public static void main(String[] args) throws FileNotFoundException {
        ArrayList<Data> list = new ArrayList<>();
        Scanner input = new Scanner(new File("data.txt"));
        while (input.hasNext()) {
            list.add(new Data(input.next(), Double.parseDouble(input.next()), Integer.parseInt(input.next())));
        }
        System.out.println("输入型号:");
        String type = new Scanner(System.in).nextLine();
        double sum = 0;
        for (Data data : list) {
            if (data.getId().equals(type))
                sum += data.getMoney();

        }
        System.out.println("型号为" + type + "的商品销售总额为" + sum);

    }

}

截图

img

注意 这个文件data.txt 是相对路径

img

请放项目根目录下