- Java code
import java.util.Scanner;import java.util.Map;import java.util.HashMap;public class Test { //private Object k ; Map <Object,Double> map = new HashMap<Object,Double> (); //无效 private Double ks ; /** * test map */ public void testmap (){ map.put("diyige", 10.0); map.put("dierge", 100.0); map.put("disange", 1000.0); map.put("disige", 10000.0); map.put("diwuge", 100000.0); //无效 for (int i=map.size()-1;i>0;i--){ for (Map.Entry<Object, Double> entry : map.entrySet() ) { System.out.println(entry.getKey()+">>>>>>>>>>>>>"+entry.getValue()); //无效累加 ks = entry.getValue()+i; } //无效 System.out.println(ks); } //无效 System.out.println(ks); // 无效 } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Test t = new Test (); t.testmap(); }}
求累加后的Value,谢谢。
------解决方案--------------------------------------------------------
你这个每次都只是赋值没有累加啊 ks = entry.getValue()+i 这步是你说的累加吗?每次都是从新赋值没有累加 累加应该是 ks = entry.getValue()+ks
------解决方案--------------------------------------------------------
- Java code
import java.util.Map;import java.util.HashMap;public class MapText{ // private Object k ; Map<Object, Double> map = new HashMap<Object, Double>(); private Double ks = 0.0; /** * test map */ public void testmap() { map.put("diyige", 10.0); map.put("dierge", 100.0); map.put("disange", 1000.0); map.put("disige", 10000.0); map.put("diwuge", 100000.0);// for (int i = map.size() - 1; i > 0; i--)// { for (Map.Entry<Object, Double> entry : map.entrySet()) { System.out.println(entry.getKey() + ">>>>>>>>>>>>>" + entry.getValue()); ks = entry.getValue() + ks; } System.out.println(ks);// } System.out.println(ks); } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub MapText t = new MapText(); t.testmap(); }}