我正在编写一个获取奥运奖牌数据的程序,并打印出各种信息。这个特殊部分在逻辑和结构方面证明有点困难。我正在尝试使用String array
和int array
(都是在main方法中创建的),将它们传递给新方法,并打印出有关它们的信息。
我的数组如下(请注意字符串数组按字母顺序排列):
String[] country = {"Afghanistan", "Afghanistan", "Canada", "China", "Italy", "Mexico"};
int[] totalMedals = {1, 2, 1, 1, 3, 2};
我想打印出来如下:
Afghanistan, 3 medal(s)
Canada, 1 medal(s)
China, 1 medal(s)
Italy, 3 medal(s)
Mexico, 2 medal(s)
我可以轻松编写以此格式列出的程序,但出现多次的国家(在此示例中...阿富汗)列出的次数与阵列中出现的次数相同。如上所述,我的输出是:
Afghanistan, 1 medal(s)
Afghanistan, 2 medal(s)
Canada, 1 medal(s)
China, 1 medal(s)
Italy, 3 medal(s)
Mexico, 2 medal(s)
阿富汗被列入两次(为了给出一个很大的背景,这是该计划的一小部分。该计划实际上也包含了运动员的名字,但这是以不同的方式处理的。)
任何人都可以提供一些关于打印出我想要的输出的最佳方法的帮助吗?
import java.util.*;
import java.io.*;
public class Project {
public static void main(String[] args) {
String[] country = {"Afghanistan", "Afghanistan", "Canada", "China", "Italy", "Mexico"};
int[] totalMedals = {1, 2, 1, 1, 3, 2};
listCountryMedals(country, totalMedals);
}
//List the grand total of medals for each country...display name only once
public static void listCountryMedals(String[] country, int[] totalMedals) {
for (int i = 0; i < country.length; i++) {
System.out.println(country[i] + ", " + totalMedals[i] + " medal(s)");
}
}
答案 0 :(得分:1)
试试这个,
public static void listCountryMedals(String[] country, int[] totalMedals)
{
Map<String, Integer> countryMap = new HashMap<String, Integer>();
for (int i = 0; i < country.length; i++)
{
Integer medals = countryMap.get(country[i]);
Integer sum = (medals == null) ? totalMedals[i] : (totalMedals[i] + medals);
countryMap.put(country[i], sum);
}
for (Map.Entry<String, Integer> countryMedals : countryMap.entrySet())
{
System.out.println(countryMedals.getKey() + ", " + countryMedals.getValue() + " medal(s)");
}
}
答案 1 :(得分:1)
假设您只想使用数组和循环,您可以执行以下操作:
public static void listCountryMedals(String[] country, int[] totalMedals) {
boolean[]seen = new boolean[country.length];
for (int i = 0; i < country.length - 1; i++) {
int medals = totalMedals[i];
if (!seen[i]) {
for(int j = i + 1; j < country.length; j++) {
if (country[i].equals(country[j])) {
medals += totalMedals[j];
seen[j] = true; //I already took this country
}
}
System.out.println(country[i] + ", " + medals + " medal(s)");
}
seen[i] = true;
}
}
答案 2 :(得分:0)
按如下方式更改方法。
public static void listCountryMedals(String[] country, int[] totalMedals) {
//Map to hold the conuntry anme and medal count pair
Map<String,Integer> countryNameToMedalCountMap= new HashMap<String,Integer>();
for (int i = 0; i < country.length; i++) {
if(countryNameToMedalCountMap.get(country[i]) == null) {
countryNameToMedalCountMap.put(country[i],totalMedals[i]);
} else {
int count = map.get(country[i]);
countryNameToMedalCountMap.put(country[i],count+totalMedals[i];
}
}
//Print the map
for(String counrty : countryNameToMedalCountMap.keySet()) {
System.out.println(country+" "+ countryNameToMedalCountMap.get(country)+" medal(s).");
}
}
注意:使用此方法,国家/地区名称将区分大小写。如果您想要不区分大小写,则必须设置某种机制。就像将地图键始终转换为小写一样,并在查找时执行相同操作
答案 3 :(得分:0)
你应该采取面向对象的方法:
public class Country {
private String name;
private int medals;
//getters/setters/constructors
}
然后变得容易:
List<Country> countries = new ArrayList<>();
//when you want to reference/print those:
for (Country c : countries) {
System.out.println(String.format("%s, %d medals(s)", c.getName(), c.getMedals()));
}
答案 4 :(得分:0)
我会用LinkedHashMap
按国家/地区对奖牌求和:
//List the grand total of medals for each country...display name only once
public static void listCountryMedals(String[] country, int[] totalMedals) {
final Map<String, Integer> map = new LinkedHashMap<String, Integer>();
for (int i = 0; i < country.length; i++) {
if (!map.containsKey(country[i]))
map.put(country[i], 0);
map.put(country[i], map.get(country[i]) + totalMedals[i]);
}
for (final Entry<String, Integer> entry : map.entrySet()) {
System.out.println(entry.getKey() + ", " + entry.getValue() + " medal(s)");
}
}
与经典HashMap
相反,LinkedHashMap
将在迭代时保留键顺序。您的国家/地区目前似乎已排序,因此您可能希望在打印时将其保留在此顺序中。
答案 5 :(得分:0)
您可以使用map来存储这些信息,将country作为关键字,将medal count作为值。
public static void listCountryMedals(String[] country, int[] totalMedals)
{
Map<String, Integer> countryMedalMap = new LinkedHashMap<String, Integer>();
for (int i = 0; i < country.length; i++)
{
String countryName = country[i];
/**
* If the country is already in the map, get the number of medal and add it up with value
* from totalMedals array for that country. Then put it back to map with the new medal count.
*/
if(countryMedalMap.containsKey(countryName))
{
int medalCount = countryMedalMap.get(countryName) + totalMedals[i];
countryMedalMap.put(countryName, medalCount);
}
/**
* If the country is not in the map, put it into map with its medal count.
*/
else
{
countryMedalMap.put(countryName, totalMedals[i]);
}
}
printMap(countryMedalMap);
}
/**
* Print the map
*
* @param map
*/
public static void printMap(Map<String, Integer> map)
{
for (String countryName : map.keySet())
{
System.out.println(String.format("%s, %s medal(s)", countryName, map.get(countryName)));
}
}