Java帮助-输出正确格式

Java帮助-输出正确格式,java,Java,编程新手至今仍掌握基本方面的知识,但迄今为止已经能够写出以下内容: public static void main(String[] args) throws FileNotFoundException { Scanner sc = new Scanner(new File("Path://pay.txt")); String name; int price; int quantity; int total; while(sc.hasNext(

编程新手至今仍掌握基本方面的知识,但迄今为止已经能够写出以下内容:

public static void main(String[] args) throws FileNotFoundException {
    Scanner sc = new Scanner(new File("Path://pay.txt"));

    String name;
    int price;
    int quantity;
    int total;

    while(sc.hasNext()){
        name = sc.next();
        price = sc.nextInt();
        quantity = sc.nextInt();

        total = price * quantity;


        System.out.println(name + " : " + total);
    }
    sc.close();

}
输出:

Emma : $16
Sofia : $8
Olivia : $70
Emma : $9
Emma : $9
Emma : $4
Emma : $120
Sofia : $33
Emma : $78
Emma : $40
Sofia : $32
Olivia : $8
Sofia : $6
Olivia : $36
Emma : $9
Emma : $45
Emma : $54
Emma : $12
Emma : $78
Emma : $36
Olivia : $4
Emma : $64
Sofia : $42
BUILD SUCCESSFUL (total time: 1 second)
我想输出每个人的总销售额,而不是像它那样显示每一行的个人总销售额

  Income
  Emma: $xx
  Sofia: $yy
  Olivia: $zz

谁能帮我一下,指引我正确的方向?我觉得我很接近,但不是100%确定


感谢您花时间阅读这篇文章。

为姓名创建一个ArrayList,为总数创建另一个ArrayList。 当您从用户处获得新输入时,最初两个arraylist都为空,您将向arraylist添加名称,并向arraylist添加总数。 但是,在这之后,每次输入一个输入时,检查arraylist中是否存在该名称,如果没有添加,如果有,则获取索引,并在total arraylist中使用该索引检索数字并向其添加新值。然后在输入所有值后,循环遍历ArrayList并打印它们

Scanner sc = new Scanner(new File("Path://pay.txt"));
    ArrayList<String> names = new ArrayList<String>();
    ArrayList<Integer> total = new ArrayList<Integer>();

    String name;
    int price;
    int quantity;


    while(sc.hasNext()){
        name = sc.next();

        price = sc.nextInt();
        quantity = sc.nextInt();

        if(names.contains(name)){
            int index = names.indexOf(name);
            int totalTemp = total.get(index);
            totalTemp += price * quantity;
            total.remove(index);
            total.add(totalTemp,index);
        }else{
            names.add(name);
            total.add(price*quantity);
        }
    }
    sc.close();
    for(int i=0;i<names.size();i++){
    System.out.println(names.get(i)+": "+total.get(i));
Scanner sc=新扫描仪(新文件(“Path://pay.txt"));
ArrayList name=新的ArrayList();
ArrayList总计=新的ArrayList();
字符串名;
国际价格;
整数;
while(sc.hasNext()){
name=sc.next();
价格=sc.nextInt();
数量=sc.nextInt();
if(name.contains(name)){
int index=names.indexOf(name);
int TOTALTTEMP=总计获取(索引);
totalTemp+=价格*数量;
总计。删除(索引);
总计。添加(总计温度,索引);
}否则{
名称。添加(名称);
加总(价格*数量);
}
}
sc.close();

对于(int i=0;这里有两种选择:使用
HashMap
,或者创建一个包含字符串和double的类,并创建这种类型的ArrayList。无论采用哪种方法,都需要在收集值时求和,然后在最后将其全部打印出来。我猜有一种流式处理技术可以帮助解决这两种问题。“我觉得我很接近,但不是100%确定。"-好吧,试试你认为你应该做的,如果不起作用,请随时回来问一个更具体的问题……不,你还没有完全做到。如果你刚刚开始,这可能会有点困难。基本上你需要做的是检查输入的所有结果,并将每个名字的所有总数相加。eas跟踪总数的最有效的方法可能是使用Java的
Map
对象之一。但是,您需要让他们输入所有数据,将数据存储在列表中,然后遍历并打印。不过,这个问题对于该网站来说相当广泛;这更适合于像这样的讨论媒体Google+或Reddit。您肯定需要至少阅读阵列(这将提供一个粗略的解决方案),但也可能是其他基本数据结构。我的建议是遵循一些教程,而不是试图通过从任务开始学习编程,并试图从中找出如何进行编程的细节。除非您已经知道此类数据结构,否则在这种情况下,您至少应该能够想出这样的方法告诉我从这里到哪里的想法。或者你可以使用其他人提到的HashMap。平行列表?哎哟。