2014-12-19 59 views
1

我是HashMaps的新成員,在幫助我理解如何在地圖中添加和打印整數。但是,不要得到我想要的東西,我會得到一些奇怪的和不同的東西,我不知道如何解決。嘗試在HashMap中添加整數但不起作用

輸出

Rick Grimes 
Rick Grimes rescued Carl 100pts! 
Rick Grimes rescued Carl 100pts!Rick Grimes rescued Michone 50pts! 
Characters rescued: Carl, Michone Total points: 10050 

正如你可能會注意到,總積分僅僅是一個分數的組合。這就是我要

所需的輸出

Rick Grimes 
Rick Grimes rescued Carl 100pts! 
Rick Grimes rescued Michone 50pts! 
Characters rescued: Carl, Michone Total points: 150 

另外一個問題,你可能在我的輸出注意什麼是line 3重複第一救援line 2但首先我希望把重點放在第一個問題。

這裏是我的代碼

GameDriver.java

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

public class GameDriver { 

    public static void main(String[] args) throws FileNotFoundException { 

     Hero N = new Cop(); 
     String s; 
     File f = new File (args[0]); 
     Scanner input = new Scanner(f); 
     while (input.hasNext()) { 
       s = input.next(); 
       if (s.equalsIgnoreCase("h")) { 
               System.out.println(N.getHeroName()); 
              } 

       if (s.equalsIgnoreCase("r")) { 
               String cn = input.next(); 
               int pts = input.nextInt(); 
               N.newMap.put(cn, pts); 
               System.out.println(N.rescue()); 

              } 

       if (s.equalsIgnoreCase("v")) { 
               System.out.println(N.view()); 
              } 
     } 

    } 
} 

求和發生在這個類

Hero.java

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

public abstract class Hero extends Character 
{ 
    private String heroname1; 
    public Hero() { 
      heroname1 = "Rick Grimes"; //the default player name 
    } 
    HashMap<String, Integer> newMap = new HashMap<String, Integer>(); 

    public Hero(String newhero) { 
      if (newhero.length() > 0) { 
       heroname1 = newhero; 
      } else { heroname1 = "Rick Grimes"; } //defaulted as protagonist 
    } 

    public String getHeroName() { 
      return heroname1; //return the name 
    } 

    public String rescue() { //class to rescue people or things 
      String out = "".toString(); 
      for(String _key : newMap.keySet()) { 
       out += heroname1 + " rescued " + _key + " " + newMap.get(_key) +! 
      } 
      return out; 
    } 

    public String toString() { //print 
      return heroname1; 

    } 
    public String view() { 
      Set<String> names = newMap.keySet(); 
      StringBuilder sb = new StringBuilder("Characters rescued: "); 
      int i = 0; 
      for (String name : names) { 
       if (i == 0) { 
        sb.append(name); 
        i++; 
       } else { 
        sb.append(", "); 
        sb.append(name); 
       } 
      } 

      String printNames = sb.toString(); 
      Collection<Integer> points = newMap.values(); 
      StringBuilder sp = new StringBuilder("Total points: "); 
      int sum = 0; 
      for (Integer value : points) { 
       sum += value; 
       sp.append(value); 
      } 
      String printPoints = sp.toString(); 
      return printNames + " " + printPoints ; 
    } 
} 

我是什麼做錯了?請要求澄清! 謝謝!

+0

在人們解決了您問到的問題後,請勿修改您的問題以更改問題。單獨提出一個新問題,並保留原來的問題。 – khelwood 2014-12-19 08:17:30

+0

好的。我覺得這將是皺眉 – Junikin 2014-12-19 08:19:58

回答

3

你只是附加的文字在這裏:

StringBuilder sp = new StringBuilder("Total points: "); 
      int sum = 0; 
      for (Integer value : points) { 
       sum += value; 
       sp.append(value);//you're wrong here. 
      } 

您應變量之和相加後追加。

StringBuilder sp = new StringBuilder("Total points: "); 
      int sum = 0; 
      for (Integer value : points) { 
       sum += value; 
      } 
sp.append(sum); 

此外,附加值應該是'sum'而不是'value';

+0

啊完美謝謝你。我能不能多說一點,看看我的代碼,並告訴我爲什麼在我的輸出中重複第一次救援?沒關係,如果你不這樣做,我也無法弄清楚 – Junikin 2014-12-19 08:09:09

相關問題