gpt4 book ai didi

java - 尝试在 HashMap 中添加整数但不起作用

转载 作者:行者123 更新时间:2023-11-29 05:13:09 24 4
gpt4 key购买 nike

我是 HashMaps 的新手,有人帮助我理解了如何在映射中添加和打印整数。然而,我并没有得到我想要的东西,而是得到了一些我不确定如何修复的奇怪和不同的东西。

输出

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

您可能会在我的输出中注意到的另一个问题是 第 3 行 重复了 第 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());
}
}

}
}

求和发生在这个类

英雄.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 ;
}
}

我做错了什么?请要求任何澄清! 谢谢!!

最佳答案

您只需在此处附加文本:

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';

关于java - 尝试在 HashMap 中添加整数但不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27561906/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com