在数组上开始java。试图对重复的金额进行分组
我正试图浏览一份有金额的名单 一个数组包含此人的姓名,另一个数组包含此人提供的金额,即john,55 sally 40 john 33 sarah 55 我的目标是把这些相似的名字加起来,打印出这个人的名字和给出的总数 约翰捐了两次,所以他总共应该有88次。但是我得到了正确的总数,但是我的程序打印了两次名字。所以john 88打印了两次。。。我知道这很可能,因为我把它放在第一个循环中,因为它迭代数组的整个长度 但我不确定如何解决这个问题在数组上开始java。试图对重复的金额进行分组,java,arrays,Java,Arrays,我正试图浏览一份有金额的名单 一个数组包含此人的姓名,另一个数组包含此人提供的金额,即john,55 sally 40 john 33 sarah 55 我的目标是把这些相似的名字加起来,打印出这个人的名字和给出的总数 约翰捐了两次,所以他总共应该有88次。但是我得到了正确的总数,但是我的程序打印了两次名字。所以john 88打印了两次。。。我知道这很可能,因为我把它放在第一个循环中,因为它迭代数组的整个长度 但我不确定如何解决这个问题 import java.util.*; public c
import java.util.*;
public class chapterfive {
public static void main (String[]args) {
Scanner in = new Scanner (System.in);
String[]names = new String[4];
int[] scores = new int[4];
for (int i = 0; i<names.length; i++) {
names[i] = in.next();
scores[i] = in.nextInt();
}
int amount = 0;
String firstname = "";
for (int i = 0; i < names.length; i++) {
for (int j=0; j < names.length; j++) {
if (names[j].equals(names[i]))
amount += scores[j];
}
System.out.println(names[i] + " " + amount);
amount = 0;
}
}
}
import java.util.*;
公共课第五章{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(系统输入);
字符串[]名称=新字符串[4];
int[]分数=新的int[4];
对于(inti=0;i你可以看到它们之间有一种类似于Name->Score的关系,所以如果你想得更抽象一些,这是一个包含Key(Name)和Value(Score)的字典,因此您可以使用另一种数据结构,如映射
,或者您可以使用数组并生成一个类Person,使用ArrayOrder,并在添加新Person时检查数组中是否存在该人员
例如:
Map <String , Integer> people = new HashMap<>();
for (int i=0; i<lengthYouWant; i++)
{
String name=in.next();
int score=in.nextInt();
if(people.contains(name)){
score= people.get(name)+score;
}
people.put(name,score);
}
Map people=newhashmap();
对于(inti=0;i你可以看到它们之间有一种类似于Name->Score的关系,所以如果你想得更抽象一些,这是一个包含Key(Name)和Value(Score)的字典,因此您可以使用另一种数据结构,如映射
,或者您可以使用数组并生成一个类Person,使用ArrayOrder,并在添加新Person时检查数组中是否存在该人员
例如:
Map <String , Integer> people = new HashMap<>();
for (int i=0; i<lengthYouWant; i++)
{
String name=in.next();
int score=in.nextInt();
if(people.contains(name)){
score= people.get(name)+score;
}
people.put(name,score);
}
Map people=newhashmap();
对于(int i=0;i如果您为Person类创建一个数组或列表,您可以实现Compariable并添加一个方法来帮助排序。如果您为Person类创建一个数组或列表,您可以实现Compariable并添加一个方法来帮助排序。Java是一种面向对象的语言,这意味着,除其他外,您可以使拥有自己的数据结构。使用并行数组容易出错,而且它会将您想要保存在一起的数据分离。那么,您需要如何组织这些数据
第一种是储存姓名和金额的方法,称之为捐赠
class Donation {
private final String name;
private final int amount;
public Donation(String name, String amount) {
this.name = name;
this.amount = amount;
// EXERCISE: Add error checking.
}
public String getName() { return name; }
public int getAmount() { return amount; }
public String toString() {
return "Name: " + name +", amount: " + amount;
}
}
请注意,此类的变量是最终的;一旦设置,它们就不能更改。它们在构造函数中设置,并且有get方法和toString方法,可以替换System.out.println
语句中的变量
接下来,您需要一种存储数据的方法。不要使用数组。List
s更灵活
private static List<Donation> donations = new ArrayList<Donation>();
// and in main:
while (true) {
String name = null;
int amount = 0;
if (in.hasNext()) {
name = in.next();
} else {
break;
}
if (in.hasNextInt()) {
amount = in.nextInt();
} else {
break;
}
donations.add(new Donation(name, amount));
} See -- no 4s.
最后,您迭代地图并打印每个条目
for ( Map.Entry<String, Integer> entry: totals.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
for(Map.Entry:totals.entrySet()){
System.out.println(entry.getKey()+“:”+entry.getValue());
}
现在,另一个练习和策略是:停止在main
中做任何事情。给出你的第五章类实例变量和方法。然后,为这些变量和方法编写测试。尝试对我的建议进行改进。然后,看看是否有库可以帮助你。Java是一种面向对象的语言,这意味着除此之外,您还可以创建自己的数据结构。使用并行阵列很容易出错,而且它会将您希望保存在一起的数据分离开来。那么,您需要如何组织这些数据呢
第一种是储存姓名和金额的方法,称之为捐赠
class Donation {
private final String name;
private final int amount;
public Donation(String name, String amount) {
this.name = name;
this.amount = amount;
// EXERCISE: Add error checking.
}
public String getName() { return name; }
public int getAmount() { return amount; }
public String toString() {
return "Name: " + name +", amount: " + amount;
}
}
请注意,此类的变量是最终的;一旦设置,它们就不能更改。它们在构造函数中设置,并且有get方法和toString方法,可以替换System.out.println
语句中的变量
接下来,您需要一种存储数据的方法。不要使用数组。List
s更灵活
private static List<Donation> donations = new ArrayList<Donation>();
// and in main:
while (true) {
String name = null;
int amount = 0;
if (in.hasNext()) {
name = in.next();
} else {
break;
}
if (in.hasNextInt()) {
amount = in.nextInt();
} else {
break;
}
donations.add(new Donation(name, amount));
} See -- no 4s.
最后,您迭代地图并打印每个条目
for ( Map.Entry<String, Integer> entry: totals.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
for(Map.Entry:totals.entrySet()){
System.out.println(entry.getKey()+“:”+entry.getValue());
}
现在,另一个练习和策略是:停止在main
中做任何事情。给你的第五章类实例变量和方法。然后,为这些变量和方法编写测试。尝试对我的建议进行改进。然后,看看是否有库可以帮助你。应该使用映射来简化事情,而不是n跟踪两个阵列。但这里有一个可能有效的修复方法(尚未测试)
String firstname=”“;
for(int i=0;ij){
跳过=真;
打破
}
else如果(名称[j].equals(名称[i])){
金额+=分数[j];
}
}
如果(!跳过){
System.out.println(名称[i]+“”+金额);
}
}
应该使用映射来简化事情,而不是跟踪两个数组。但是这里有一个可能有效的修复方法(尚未测试)
String firstname=”“;
for(int i=0;ij){
跳过=真;
打破
}
else如果(名称[j].equals(名称[i])){
金额+=分数[j];
}
}
如果(!跳过){
System.out.println(名称[i]+“”+金额);
}
}
感谢您的快速回复…这不起作用,因为金额必须是总数。例如,我有四个条目:john 55 john 33 sarah 22 sally 44正在打印:john 88 john 88 sarah 22 sally 44/It