当前位置: 代码迷 >> Eclipse >> 关于相同名字累加的有关问题
  详细解决方案

关于相同名字累加的有关问题

热度:19   发布时间:2016-04-23 13:28:58.0
关于相同名字累加的问题!
import java.util.Scanner;
public class helloworld2 {
  /**
  * @param args
  */
  public static void main(String[] args) {
  // TODO Auto-generated method stub
  Scanner scanner = new Scanner(System.in);
  int n,total = 0;
  String name;
  System.out.println("Enter the number of expenses:");
  n = scanner.nextInt();
  StringBuilder sBuilder = new StringBuilder();
  for(int i = 0; i < n; i++){
  System.out.println("Expense Name:");
  name = scanner.next();
  System.out.println("Enter Cost: ");
  int cost = scanner.nextInt();
  total += cost;
  sBuilder.append("\nExpense Name:" + name + " Cost: " + cost);
  }  
  System.out.println(sBuilder.toString() + "\nTotal Cost:" + total);
  System.exit(0);
  }
}

这是我算的一个工资计算的JAVA程序。
现在有个问题:我输入相同的名字,它是分开显示的,而不是加在一起。
举个例子:比如说我输入两次人名叫Tom的人以及其工资,程序会显示两次Tom,而不是将其合并。

请问如何解决这个问题?
谢谢各位!

------解决方案--------------------
LZ再试一下。
Java code
 public static void main(String[] args) throws Exception {        // TODO Auto-generated method stub        Scanner scanner = new Scanner(System.in);        int n, total = 0;        String name;        System.out.println("Enter the number of expenses:");        n = scanner.nextInt();        StringBuilder sBuilder = new StringBuilder();        List<String> lst = new ArrayList<String>();        for (int i = 0; i < n; i++) {            System.out.println("Expense Name:");            name = scanner.next();            System.out.println("Enter Cost: ");            int cost = scanner.nextInt();            total += cost;            if ( !lst.contains(name)){                lst.add(name);               sBuilder.append("\nExpense Name:" + name + " Cost: " + cost);            } else {                continue;            }                   }        System.out.println(sBuilder.toString() + "\nTotal Cost:" + total);        System.exit(0);    }
------解决方案--------------------
Java code
import java.util.HashMap;import java.util.Map;import java.util.Scanner;public class helloworld2 {    public static void main(String[] args) {        // TODO Auto-generated method stub        Scanner scanner = new Scanner(System.in);        int n, total = 0;        String name;        System.out.println("Enter the number of expenses:");        n = scanner.nextInt();        Map<String, Integer> costMap = new HashMap<String, Integer>();        for (int i = 0; i < n; i++) {            System.out.println("Expense Name:");            name = scanner.next();            System.out.println("Enter Cost: ");            int cost = scanner.nextInt();            if (costMap.containsKey(name)) {                costMap.put(name, costMap.get(name) + cost);            } else {                costMap.put(name, cost);            }        }        for (String key : costMap.keySet()) {            System.out.println("Expense Name:" + key + " Cost: " + costMap.get(key));            total += costMap.get(key);        }        System.out.println("Total Cost:" + total);        System.exit(0);    }}
  相关解决方案